1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Controller\Traits\Query; |
13
|
|
|
|
14
|
|
|
use Phalcon\Messages\Message; |
15
|
|
|
use Phalcon\Mvc\ModelInterface; |
16
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractExpose; |
17
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractInjectable; |
18
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractModel; |
19
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractParams; |
20
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractQuery; |
21
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\Query\AbstractSave; |
22
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\Query\AbstractWith; |
23
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\Query\Fields\AbstractMapFields; |
24
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\Query\Fields\AbstractSaveFields; |
25
|
|
|
use Zemit\Mvc\Model\Interfaces\EagerLoadInterface; |
26
|
|
|
|
27
|
|
|
trait Save |
28
|
|
|
{ |
29
|
|
|
use AbstractSave; |
30
|
|
|
|
31
|
|
|
use AbstractExpose; |
32
|
|
|
use AbstractInjectable; |
33
|
|
|
use AbstractModel; |
34
|
|
|
use AbstractParams; |
35
|
|
|
use AbstractQuery; |
36
|
|
|
use AbstractWith; |
37
|
|
|
use AbstractMapFields; |
38
|
|
|
use AbstractSaveFields; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Saves the entity and returns the result as an array. |
42
|
|
|
* |
43
|
|
|
* @return array The result of the save operation, including the saved status, messages, |
44
|
|
|
* and optionally the saved entity data if the save was successful. |
45
|
|
|
*/ |
46
|
|
|
public function save(): array |
47
|
|
|
{ |
48
|
|
|
$post = $this->getParams(); |
49
|
|
|
$model = $this->findFirst(); |
50
|
|
|
$saveFields = $this->getSaveFields()?->toArray(); |
51
|
|
|
$mapFields = $this->getMapFields()?->toArray(); |
52
|
|
|
|
53
|
|
|
if (isset($post['id']) && !isset($model)) { |
54
|
|
|
return [ |
55
|
|
|
'saved' => false, |
56
|
|
|
'messages' => [new Message('Entity id `' . $post['id'] . '` not found.', 'id', 'NotFound', 404)], |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
unset($post['id']); |
60
|
|
|
|
61
|
|
|
$model ??= $this->loadModel(); |
62
|
|
|
assert($model instanceof ModelInterface); |
63
|
|
|
|
64
|
|
|
// before assign event |
65
|
|
|
$this->eventsManager->fire('rest:beforeAssign', $this, [&$model, &$post, &$saveFields, &$mapFields], false); |
66
|
|
|
$model->assign($post, $saveFields, $mapFields); |
67
|
|
|
|
68
|
|
|
if ($this->eventsManager->fire('rest:beforeSave', $this, [&$model]) === false) { |
69
|
|
|
return [ |
70
|
|
|
'saved' => false, |
71
|
|
|
'messages' => $model->getMessages(), |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
$saved = $model->save(); |
75
|
|
|
$this->eventsManager->fire('rest:afterSave', $this, [&$model], false); |
76
|
|
|
|
77
|
|
|
$ret = [ |
78
|
|
|
'saved' => $saved, |
79
|
|
|
'messages' => $model->getMessages() |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
if ($saved) { |
83
|
|
|
// load relationship |
84
|
|
|
$with = $this->getWith()?->toArray(); |
85
|
|
|
if (isset($with) && $model instanceof EagerLoadInterface) { |
86
|
|
|
$model = $model->load($with); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// expose the model data |
90
|
|
|
$ret['data'] = $this->expose($model); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $ret; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|