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
|
|
|
* @todo handle dynamic and/or composed primary keys |
43
|
|
|
* @todo handle multiple entities |
44
|
|
|
* @todo segregate create vs update vs save |
45
|
|
|
* |
46
|
|
|
* @return array The result of the save operation, including the saved status, messages, |
47
|
|
|
* and optionally the saved entity data if the save was successful. |
48
|
|
|
*/ |
49
|
|
|
public function save(): array |
50
|
|
|
{ |
51
|
|
|
$post = $this->getParams(); |
52
|
|
|
$saveFields = $this->getSaveFields()?->toArray(); |
53
|
|
|
$mapFields = $this->getMapFields()?->toArray(); |
54
|
|
|
|
55
|
|
|
// @todo find what we should do here to handle: 1. dynamic and/or composed primary keys |
56
|
|
|
if (isset($post['id'])) { |
57
|
|
|
$model = $this->findFirst(); |
58
|
|
|
if (!isset($model)) { |
59
|
|
|
return [ |
60
|
|
|
'saved' => false, |
61
|
|
|
'messages' => [new Message('Entity id `' . $post['id'] . '` not found.', 'id', 'NotFound', 404)], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
unset($post['id']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$model ??= $this->loadModel(); |
|
|
|
|
68
|
|
|
assert($model instanceof ModelInterface); |
69
|
|
|
|
70
|
|
|
// before assign event |
71
|
|
|
$this->eventsManager->fire('rest:beforeAssign', $this, [&$model, &$post, &$saveFields, &$mapFields], false); |
72
|
|
|
$model->assign($post, $saveFields, $mapFields); |
73
|
|
|
|
74
|
|
|
if ($this->eventsManager->fire('rest:beforeSave', $this, [&$model]) === false) { |
|
|
|
|
75
|
|
|
return [ |
76
|
|
|
'saved' => false, |
77
|
|
|
'messages' => $model->getMessages(), |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
$saved = $model->save(); |
81
|
|
|
$this->eventsManager->fire('rest:afterSave', $this, [&$model], false); |
82
|
|
|
|
83
|
|
|
$ret = [ |
84
|
|
|
'saved' => $saved, |
85
|
|
|
'messages' => $model->getMessages() |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
if ($saved) { |
89
|
|
|
// load relationship |
90
|
|
|
$with = $this->getWith()?->toArray(); |
91
|
|
|
if (isset($with) && $model instanceof EagerLoadInterface) { |
92
|
|
|
$model = $model->load($with); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// expose the model data |
96
|
|
|
$ret['data'] = $this->expose($model); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $ret; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|