1 | <?php |
||
23 | abstract class Model implements Arrayable, Jsonable, Stringable, JsonSerializable |
||
24 | { |
||
25 | use Presentable, Eventable, Mergeable; |
||
26 | use Idable, Userable, Timestampable; |
||
27 | use Fillable, Cacheable; |
||
28 | |||
29 | /** |
||
30 | * @var IDAL |
||
31 | */ |
||
32 | public $_dal; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | public $_exist = false; |
||
38 | |||
39 | /** |
||
40 | * Create a new instance. |
||
41 | * |
||
42 | * @param IDAL $dal |
||
43 | * @param array $attributes |
||
44 | */ |
||
45 | 52 | public function __construct(IDAL $dal, array $attributes = []) |
|
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function toArray() |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function jsonSerialize() |
||
69 | |||
70 | /** |
||
71 | * Save the model. |
||
72 | * |
||
73 | * @param array $columns |
||
74 | * @return bool |
||
75 | */ |
||
76 | 24 | public function save($columns = ['*']) |
|
77 | { |
||
78 | 24 | $columns = $columns ? (array)$columns : ['*']; |
|
79 | |||
80 | 24 | if ($this->saving() === false) { |
|
81 | return false; |
||
82 | } |
||
83 | |||
84 | 24 | $this->fillTimestamp(); |
|
85 | |||
86 | 24 | $this->_dal->put($columns); |
|
87 | |||
88 | 24 | $this->_exist = true; |
|
89 | |||
90 | // self::cache()->put($id, $this); |
||
91 | |||
92 | 24 | if ($this->saved() === false) { |
|
93 | return false; |
||
94 | } |
||
95 | |||
96 | 24 | return true; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Delete the model. |
||
101 | * |
||
102 | * @return bool |
||
103 | */ |
||
104 | 6 | public function delete() |
|
123 | |||
124 | /** |
||
125 | * Create a new instance. |
||
126 | * |
||
127 | * @return static |
||
128 | */ |
||
129 | public static function createInstance() |
||
133 | |||
134 | /** |
||
135 | * Find a model by its primary key. |
||
136 | * |
||
137 | * @param mixed $id |
||
138 | * @param array $columns |
||
139 | * @param array $options |
||
140 | * @return static |
||
141 | */ |
||
142 | 22 | public static function find($id, array $columns = ['*'], $options = []) |
|
175 | |||
176 | /** |
||
177 | * Find a model by its primary key or return new model. |
||
178 | * |
||
179 | * @param mixed $id |
||
180 | * @return static |
||
181 | */ |
||
182 | 2 | public static function findOrNew($id) |
|
191 | |||
192 | /** |
||
193 | * Find a model by its primary key or throw an exception. |
||
194 | * |
||
195 | * @param mixed $id |
||
196 | * @param array $columns |
||
197 | * @param int $parent |
||
198 | * @return static |
||
199 | */ |
||
200 | 4 | public static function findOrFail($id, array $columns = ['*'], $parent = null) |
|
208 | |||
209 | /** |
||
210 | * Save a new model and return the instance. |
||
211 | * |
||
212 | * @param array $attributes |
||
213 | * @throws Exception |
||
214 | * @return static |
||
215 | */ |
||
216 | 8 | public static function create(array $attributes = []) |
|
230 | |||
231 | /** |
||
232 | * Destroy the models by the given id. |
||
233 | * |
||
234 | * @param mixed $id |
||
235 | */ |
||
236 | 2 | public static function destroy($id) |
|
246 | } |
||
247 |