3 namespace Civi\Api4\Generic
;
5 use Civi\API\Exception\NotImplementedException
;
8 * Update one or more records with new values.
10 * Use the where clause (required) to select them.
12 class BasicUpdateAction
extends AbstractUpdateAction
{
17 * Function(array $item, BasicUpdateAction $thisAction) => array
22 * BasicUpdateAction constructor.
24 * @param string $entityName
25 * @param string $actionName
26 * @param string|array $select
27 * One or more fields to select from each matching item.
28 * @param callable $setter
29 * Function(array $item, BasicUpdateAction $thisAction) => array
31 public function __construct($entityName, $actionName, $select = 'id', $setter = NULL) {
32 parent
::__construct($entityName, $actionName, $select);
33 $this->setter
= $setter;
37 * We pass the writeRecord function an array representing one item to update.
38 * We expect to get the same format back.
40 * @param \Civi\Api4\Generic\Result $result
41 * @throws \API_Exception
42 * @throws \Civi\API\Exception\NotImplementedException
44 public function _run(Result
$result) {
45 foreach ($this->getBatchRecords() as $item) {
46 $result[] = $this->writeRecord($this->values +
$item);
49 if (!$result->count()) {
50 throw new \
API_Exception('Cannot ' . $this->getActionName() . ' ' . $this->getEntityName() . ', no records found with ' . $this->whereClauseToString());
55 * This Basic Update class can be used in one of two ways:
57 * 1. Use this class directly by passing a callable ($setter) to the constructor.
58 * 2. Extend this class and override this function.
60 * Either way, this function should return an array representing the one modified object.
64 * @throws \Civi\API\Exception\NotImplementedException
66 protected function writeRecord($item) {
67 if (is_callable($this->setter
)) {
68 return call_user_func($this->setter
, $item, $this);
70 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());