Merge pull request #18281 from sunilpawar/report_48
[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 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20 namespace Civi\Api4\Generic;
21
22 /**
23 * Create a new $ENTITY from supplied values.
24 *
25 * This action will create 1 new $ENTITY.
26 * It cannot be used to update existing $ENTITIES; use the `Update` or `Replace` actions for that.
27 */
28 class DAOCreateAction extends AbstractCreateAction {
29 use Traits\DAOActionTrait;
30
31 /**
32 * @inheritDoc
33 */
34 public function _run(Result $result) {
35 $this->formatWriteValues($this->values);
36 $this->validateValues();
37 $params = $this->values;
38 $this->fillDefaults($params);
39
40 $resultArray = $this->writeObjects([$params]);
41
42 $result->exchangeArray($resultArray);
43 }
44
45 /**
46 * @throws \API_Exception
47 */
48 protected function validateValues() {
49 if (!empty($this->values['id'])) {
50 throw new \API_Exception('Cannot pass id to Create action. Use Update action instead.');
51 }
52 parent::validateValues();
53 }
54
55 }