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 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Generic
;
15 use Civi\Api4\Event\ValidateValuesEvent
;
18 * Base class for all `Update` api actions
20 * @method $this setValues(array $values) Set all field values from an array of key => value pairs.
21 * @method array getValues() Get field values.
22 * @method $this setReload(bool $reload) Specify whether complete objects will be returned after saving.
23 * @method bool getReload()
25 * @package Civi\Api4\Generic
27 abstract class AbstractUpdateAction
extends AbstractBatchAction
{
30 * Field values to update.
35 protected $values = [];
38 * Reload $ENTITIES after saving.
40 * Setting to `true` will load complete records and return them as the api result.
41 * If `false` the api usually returns only the fields specified to be updated.
45 protected $reload = FALSE;
48 * @param string $fieldName
52 public function getValue(string $fieldName) {
53 return $this->values
[$fieldName] ??
NULL;
57 * Add an item to the values array.
59 * @param string $fieldName
63 public function addValue(string $fieldName, $value) {
64 $this->values
[$fieldName] = $value;
69 * @throws \API_Exception
71 protected function validateValues() {
72 // FIXME: There should be a protocol to report a full list of errors... Perhaps a subclass of API_Exception?
73 $e = new ValidateValuesEvent($this, [$this->values
], new \
CRM_Utils_LazyArray(function () {
74 $existing = $this->getBatchAction()->setSelect(['*'])->execute();
76 foreach ($existing as $record) {
77 $result[] = ['old' => $record, 'new' => $this->values
];
81 \Civi
::dispatcher()->dispatch('civi.api4.validate', $e);
82 if (!empty($e->errors
)) {
83 throw $e->toException();