Skip to content

Commit

Permalink
APIv4 - don't throw exception when deleting 0 items
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Jan 27, 2020
1 parent c2009ff commit ff8584d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
11 changes: 3 additions & 8 deletions Civi/Api4/Generic/DAODeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,14 @@ class DAODeleteAction extends AbstractBatchAction {
*/
public function _run(Result $result) {
$defaults = $this->getParamDefaults();
if ($defaults['where'] && !array_diff_key($this->where, $defaults['where'])) {
if ($defaults['where'] && $this->where === $defaults['where']) {
throw new \API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified');
}

$items = $this->getObjects();

if (!$items) {
throw new \API_Exception('Cannot delete ' . $this->getEntityName() . ', no records found with ' . $this->whereClauseToString());
if ($items) {
$result->exchangeArray($this->deleteObjects($items));
}

$ids = $this->deleteObjects($items);

$result->exchangeArray($ids);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Civi/Test/Api3TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public function runApi4Legacy($v3Entity, $v3Action, $v3Params = []) {
break;

case 'delete':
if (!empty($v3Params['id'])) {
if (isset($v3Params['id'])) {
$v4Params['where'][] = ['id', '=', $v3Params['id']];
}
break;
Expand Down Expand Up @@ -531,7 +531,7 @@ public function runApi4Legacy($v3Entity, $v3Action, $v3Params = []) {
];
}

if (($v3Action == 'getsingle' || $v3Action == 'getvalue') && count($result) != 1) {
if (($v3Action == 'getsingle' || $v3Action == 'getvalue' || $v3Action == 'delete') && count($result) != 1) {
return $onlySuccess ? 0 : [
'is_error' => 1,
'error_message' => "Expected one $v4Entity but found " . count($result),
Expand Down

0 comments on commit ff8584d

Please sign in to comment.