3 namespace Civi\Api4\Generic
;
6 * Delete one or more items, based on criteria specified in Where param (required).
8 class DAODeleteAction
extends AbstractBatchAction
{
9 use Traits\DAOActionTrait
;
12 * Batch delete function
14 public function _run(Result
$result) {
15 $defaults = $this->getParamDefaults();
16 if ($defaults['where'] && !array_diff_key($this->where
, $defaults['where'])) {
17 throw new \
API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified');
20 $items = $this->getObjects();
23 throw new \
API_Exception('Cannot delete ' . $this->getEntityName() . ', no records found with ' . $this->whereClauseToString());
26 $ids = $this->deleteObjects($items);
28 $result->exchangeArray($ids);
34 * @throws \API_Exception
36 protected function deleteObjects($items) {
38 $baoName = $this->getBaoName();
40 if ($this->getCheckPermissions()) {
41 foreach ($items as $item) {
42 $this->checkContactPermissions($baoName, $item);
46 if ($this->getEntityName() !== 'EntityTag' && method_exists($baoName, 'del')) {
47 foreach ($items as $item) {
48 $args = [$item['id']];
49 $bao = call_user_func_array([$baoName, 'del'], $args);
51 $ids[] = ['id' => $item['id']];
54 throw new \
API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}");
59 foreach ($items as $item) {
60 $bao = new $baoName();
61 $bao->id
= $item['id'];
63 $action_result = $bao->delete();
65 $ids[] = ['id' => $item['id']];
68 throw new \
API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}");