Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv4 - Correctly return null values from DAO save actions #16645

Merged
merged 1 commit into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

}

}