Skip to content

Commit

Permalink
Merge pull request #16645 from colemanw/null
Browse files Browse the repository at this point in the history
APIv4 - Correctly return null values from DAO save actions
  • Loading branch information
colemanw authored Feb 28, 2020
2 parents 3f5cbc9 + a4c7afc commit 03cfc3c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
41 changes: 24 additions & 17 deletions Civi/Api4/Generic/Traits/DAOActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,33 @@ protected function getBaoName() {
}

/**
* Extract the true fields from a BAO
* Convert saved object to array
*
* (Used by create and update actions)
* @param object $bao
* Used by create, update & save actions
*
* @param \CRM_Core_DAO $bao
* @param array $input
* @return array
*/
public static function baoToArray($bao) {
$fields = $bao->fields();
public function baoToArray($bao, $input) {
$allFields = array_column($bao->fields(), 'name');
if (!empty($this->reload)) {
$inputFields = $allFields;
$bao->find(TRUE);
}
else {
$inputFields = array_keys($input);
// Convert 'null' input to true null
foreach ($input as $key => $val) {
if ($val === 'null') {
$bao->$key = NULL;
}
}
}
$values = [];
foreach ($fields as $key => $field) {
$name = $field['name'];
if (property_exists($bao, $name)) {
$values[$name] = isset($bao->$name) ? $bao->$name : NULL;
foreach ($allFields as $field) {
if (isset($bao->$field) || in_array($field, $inputFields)) {
$values[$field] = $bao->$field ?? NULL;
}
}
return $values;
Expand Down Expand Up @@ -160,14 +174,7 @@ protected function writeObjects($items) {
throw new \API_Exception($errMessage);
}

if (!empty($this->reload) && is_a($createResult, 'CRM_Core_DAO')) {
$createResult->find(TRUE);
}

// trim back the junk and just get the array:
$resultArray = $this->baoToArray($createResult);

$result[] = $resultArray;
$result[] = $this->baoToArray($createResult, $item);
}
FormattingUtil::formatOutputValues($result, $this->entityFields(), $this->getEntityName());
return $result;
Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/api/v4/Action/NullValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace api\v4\Action;

use Civi\Api4\Activity;
use Civi\Api4\Contact;
use api\v4\UnitTestCase;

Expand Down Expand Up @@ -71,4 +72,43 @@ public function testSettingToNull() {
$this->assertSame('ILoveMy', $contact['display_name']);
}

public function testSaveWithReload() {
$contact = Contact::create()
->setCheckPermissions(FALSE)
->addValue('first_name', 'Firsty')
->addValue('last_name', 'Lasty')
->execute()
->first();

$activity = Activity::create()
->setCheckPermissions(FALSE)
->addValue('source_contact_id', $contact['id'])
->addValue('activity_type_id', 1)
->addValue('subject', 'hello')
->execute()
->first();

$this->assertEquals('hello', $activity['subject']);

$saved = Activity::save()
->setCheckPermissions(FALSE)
->addRecord(['id' => $activity['id'], 'subject' => NULL])
->execute()
->first();

$this->assertNull($saved['subject']);
$this->assertArrayNotHasKey('activity_date_time', $saved);

$saved = Activity::save()
->setCheckPermissions(FALSE)
->addRecord(['id' => $activity['id'], 'subject' => NULL])
->setReload(TRUE)
->execute()
->first();

$this->assertNull($saved['subject']);
$this->assertArrayHasKey('activity_date_time', $saved);

}

}

0 comments on commit 03cfc3c

Please sign in to comment.