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
22 namespace Civi\Api4\Generic
;
24 use Civi\API\Exception\NotImplementedException
;
27 * Update one or more records with new values.
29 * Use the where clause (required) to select them.
31 class BasicUpdateAction
extends AbstractUpdateAction
{
36 * Function(array $item, BasicUpdateAction $thisAction) => array
41 * BasicUpdateAction constructor.
43 * @param string $entityName
44 * @param string $actionName
45 * @param string|array $select
46 * One or more fields to select from each matching item.
47 * @param callable $setter
48 * Function(array $item, BasicUpdateAction $thisAction) => array
50 public function __construct($entityName, $actionName, $select = 'id', $setter = NULL) {
51 parent
::__construct($entityName, $actionName, $select);
52 $this->setter
= $setter;
56 * We pass the writeRecord function an array representing one item to update.
57 * We expect to get the same format back.
59 * @param \Civi\Api4\Generic\Result $result
60 * @throws \API_Exception
61 * @throws \Civi\API\Exception\NotImplementedException
63 public function _run(Result
$result) {
64 foreach ($this->getBatchRecords() as $item) {
65 $result[] = $this->writeRecord($this->values +
$item);
68 if (!$result->count()) {
69 throw new \
API_Exception('Cannot ' . $this->getActionName() . ' ' . $this->getEntityName() . ', no records found with ' . $this->whereClauseToString());
74 * This Basic Update class can be used in one of two ways:
76 * 1. Use this class directly by passing a callable ($setter) to the constructor.
77 * 2. Extend this class and override this function.
79 * Either way, this function should return an array representing the one modified object.
83 * @throws \Civi\API\Exception\NotImplementedException
85 protected function writeRecord($item) {
86 if (is_callable($this->setter
)) {
87 return call_user_func($this->setter
, $item, $this);
89 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());