4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace Civi\Api4\Generic
;
23 * Update one or more $ENTITY with new values.
25 * Use the `where` clause (required) to select them.
27 class DAOUpdateAction
extends AbstractUpdateAction
{
28 use Traits\DAOActionTrait
;
31 * Criteria for selecting items to update.
33 * Required if no id is supplied in values.
37 protected $where = [];
42 public function _run(Result
$result) {
43 $this->formatWriteValues($this->values
);
44 // Add ID from values to WHERE clause and check for mismatch
45 if (!empty($this->values
['id'])) {
46 $wheres = array_column($this->where
, NULL, 0);
47 if (!isset($wheres['id'])) {
48 $this->addWhere('id', '=', $this->values
['id']);
50 elseif (!($wheres['id'][1] === '=' && $wheres['id'][2] == $this->values
['id'])) {
51 throw new \
Exception("Cannot update the id of an existing " . $this->getEntityName() . '.');
55 // Require WHERE if we didn't get an ID from values
57 throw new \
API_Exception('Parameter "where" is required unless an id is supplied in values.');
60 // Update a single record by ID unless select requires more than id
61 if ($this->getSelect() === ['id'] && count($this->where
) === 1 && $this->where
[0][0] === 'id' && $this->where
[0][1] === '=' && !empty($this->where
[0][2])) {
62 $this->values
['id'] = $this->where
[0][2];
63 $items = [$this->values
];
64 $result->exchangeArray($this->writeObjects($items));
68 // Batch update 1 or more records based on WHERE clause
69 $items = $this->getBatchRecords();
70 foreach ($items as &$item) {
71 $item = $this->values +
$item;
74 $result->exchangeArray($this->writeObjects($items));