1 | <?php |
||
16 | class TbEditableSaver extends CComponent |
||
17 | { |
||
18 | /** |
||
19 | * scenarion used in model for update |
||
20 | * |
||
21 | * @var mixed |
||
22 | */ |
||
23 | public $scenario = 'editable'; |
||
24 | |||
25 | /** |
||
26 | * name of model |
||
27 | * |
||
28 | * @var mixed |
||
29 | */ |
||
30 | public $modelClass; |
||
31 | /** |
||
32 | * primaryKey value |
||
33 | * |
||
34 | * @var mixed |
||
35 | */ |
||
36 | public $primaryKey; |
||
37 | /** |
||
38 | * name of attribute to be updated |
||
39 | * |
||
40 | * @var mixed |
||
41 | */ |
||
42 | public $attribute; |
||
43 | /** |
||
44 | * model instance |
||
45 | * |
||
46 | * @var CActiveRecord |
||
47 | */ |
||
48 | public $model; |
||
49 | |||
50 | /** |
||
51 | * @var mixed new value of attribute |
||
52 | */ |
||
53 | public $value; |
||
54 | |||
55 | /** |
||
56 | * http status code returned in case of error |
||
57 | */ |
||
58 | public $errorHttpCode = 400; |
||
59 | |||
60 | /** |
||
61 | * name of changed attributes. Used when saving model |
||
62 | * |
||
63 | * @var mixed |
||
64 | */ |
||
65 | protected $changedAttributes = array(); |
||
66 | |||
67 | /** |
||
68 | *### ._construct() |
||
69 | * |
||
70 | * Constructor |
||
71 | * |
||
72 | * @param $modelClass |
||
73 | * |
||
74 | * @throws CException |
||
75 | * @internal param mixed $modelName |
||
76 | * @return \TbEditableSaver |
||
|
|||
77 | */ |
||
78 | public function __construct($modelClass) |
||
95 | |||
96 | /** |
||
97 | *### .update() |
||
98 | * |
||
99 | * main function called to update column in database |
||
100 | * |
||
101 | */ |
||
102 | public function update() |
||
174 | |||
175 | /** |
||
176 | *### .checkErros() |
||
177 | * |
||
178 | * errors as CHttpException |
||
179 | * @internal param $msg |
||
180 | * @throws CHttpException |
||
181 | */ |
||
182 | public function checkErrors() |
||
183 | { |
||
184 | if (!$this->model->hasErrors()) |
||
185 | return; |
||
186 | |||
187 | $msg = array(); |
||
188 | foreach ($this->model->getErrors() as $attribute => $errors) { |
||
189 | // TODO: make use of $attribute elements |
||
190 | $msg = array_merge($msg, $errors); |
||
191 | } |
||
192 | $this->error($msg[0]); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | *### .error() |
||
197 | * |
||
198 | * errors as CHttpException |
||
199 | * |
||
200 | * @param $msg |
||
201 | * |
||
202 | * @throws CHttpException |
||
203 | */ |
||
204 | public function error($msg) |
||
208 | |||
209 | /** |
||
210 | *### .setAttribute() |
||
211 | * |
||
212 | * setting new value of attribute. |
||
213 | * Attrubute name also stored in array to save only changed attributes |
||
214 | * |
||
215 | * @param mixed $name |
||
216 | * @param mixed $value |
||
217 | */ |
||
218 | public function setAttribute($name, $value) |
||
225 | |||
226 | /** |
||
227 | *### .onBeforeUpdate() |
||
228 | * |
||
229 | * This event is raised before the update is performed. |
||
230 | * |
||
231 | * @param CModelEvent $event the event parameter |
||
232 | */ |
||
233 | public function onBeforeUpdate($event) |
||
237 | |||
238 | /** |
||
239 | *### .onAfterUpdate() |
||
240 | * |
||
241 | * This event is raised after the update is performed. |
||
242 | * |
||
243 | * @param CModelEvent $event the event parameter |
||
244 | */ |
||
245 | public function onAfterUpdate($event) |
||
249 | |||
250 | /** |
||
251 | *### .beforeUpdate() |
||
252 | * |
||
253 | * beforeUpdate |
||
254 | * |
||
255 | */ |
||
256 | protected function beforeUpdate() |
||
260 | |||
261 | /** |
||
262 | *### .afterUpdate() |
||
263 | * |
||
264 | * afterUpdate |
||
265 | * |
||
266 | */ |
||
267 | protected function afterUpdate() |
||
271 | } |
||
272 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.