Merge pull request #23349 from eileenmcnaughton/import_var
[civicrm-core.git] / Civi / Api4 / Generic / DAOCreateAction.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 namespace Civi\Api4\Generic;
14
15 /**
16 * Create a new $ENTITY from supplied values.
17 *
18 * This action will create 1 new $ENTITY.
19 * It cannot be used to update existing $ENTITIES; use the `Update` or `Replace` actions for that.
20 */
21 class DAOCreateAction extends AbstractCreateAction {
22 use Traits\DAOActionTrait;
23
24 /**
25 * @inheritDoc
26 */
27 public function _run(Result $result) {
28 $this->formatWriteValues($this->values);
29 $this->fillDefaults($this->values);
30 $this->validateValues();
31
32 $items = [$this->values];
33 $result->exchangeArray($this->writeObjects($items));
34 }
35
36 /**
37 * @throws \API_Exception
38 */
39 protected function validateValues() {
40 if (!empty($this->values['id'])) {
41 throw new \API_Exception('Cannot pass id to Create action. Use Update action instead.');
42 }
43 parent::validateValues();
44 }
45
46 }