getBatchRecords(); // Copy defaults from where clause if the operator is = foreach ($this->where as $clause) { if (is_array($clause) && $clause[1] === '=') { $this->defaults[$clause[0]] = $clause[2]; } } $idField = $this->getSelect()[0]; $toDelete = array_diff_key(array_column($items, NULL, $idField), array_flip(array_filter(\CRM_Utils_Array::collect($idField, $this->records)))); $saveAction = \Civi\API\Request::create($this->getEntityName(), 'save', ['version' => 4]); $saveAction ->setCheckPermissions($this->getCheckPermissions()) ->setReload($this->reload) ->setRecords($this->records) ->setDefaults($this->defaults); $result->exchangeArray((array) $saveAction->execute()); if ($toDelete) { $result->deleted = (array) civicrm_api4($this->getEntityName(), 'delete', [ 'where' => [[$idField, 'IN', array_keys($toDelete)]], 'checkPermissions' => $this->getCheckPermissions(), ]); } } /** * Set default value for a field. * @param string $fieldName * @param mixed $defaultValue * @return $this */ public function addDefault(string $fieldName, $defaultValue) { $this->defaults[$fieldName] = $defaultValue; return $this; } /** * Add one or more records * @param array ...$records * @return $this */ public function addRecord(array ...$records) { $this->records = array_merge($this->records, $records); return $this; } }