@@ -208,8 +208,14 @@ public function insert($runValidation = true, $attributes = null)
208208 if ($ runValidation && !$ this ->validate ($ attributes )) {
209209 return false ;
210210 }
211- $ result = $ this ->insertInternal ($ attributes );
212211
212+ if (!$ this ->isTransactional (self ::OP_INSERT ))
213+ return $ this ->insertInternal ($ attributes );
214+
215+ $ result = null ;
216+ static ::getDb ()->transaction (function ()use ($ attribute ,&$ result ){
217+ $ result = $ this ->insertInternal ($ attributes );
218+ });
213219 return $ result ;
214220 }
215221
@@ -243,6 +249,75 @@ protected function insertInternal($attributes = null)
243249 return true ;
244250 }
245251
252+ /**
253+ * Saves the changes to this active record into the associated database table.
254+ *
255+ * This method performs the following steps in order:
256+ *
257+ * 1. call [[beforeValidate()]] when `$runValidation` is `true`. If [[beforeValidate()]]
258+ * returns `false`, the rest of the steps will be skipped;
259+ * 2. call [[afterValidate()]] when `$runValidation` is `true`. If validation
260+ * failed, the rest of the steps will be skipped;
261+ * 3. call [[beforeSave()]]. If [[beforeSave()]] returns `false`,
262+ * the rest of the steps will be skipped;
263+ * 4. save the record into database. If this fails, it will skip the rest of the steps;
264+ * 5. call [[afterSave()]];
265+ *
266+ * In the above step 1, 2, 3 and 5, events [[EVENT_BEFORE_VALIDATE]],
267+ * [[EVENT_AFTER_VALIDATE]], [[EVENT_BEFORE_UPDATE]], and [[EVENT_AFTER_UPDATE]]
268+ * will be raised by the corresponding methods.
269+ *
270+ * Only the [[dirtyAttributes|changed attribute values]] will be saved into database.
271+ *
272+ * For example, to update a customer record:
273+ *
274+ * ```php
275+ * $customer = Customer::findOne($id);
276+ * $customer->name = $name;
277+ * $customer->email = $email;
278+ * $customer->update();
279+ * ```
280+ *
281+ * Note that it is possible the update does not affect any row in the table.
282+ * In this case, this method will return 0. For this reason, you should use the following
283+ * code to check if update() is successful or not:
284+ *
285+ * ```php
286+ * if ($customer->update() !== false) {
287+ * // update successful
288+ * } else {
289+ * // update failed
290+ * }
291+ * ```
292+ *
293+ * @param bool $runValidation whether to perform validation (calling [[validate()]])
294+ * before saving the record. Defaults to `true`. If the validation fails, the record
295+ * will not be saved to the database and this method will return `false`.
296+ * @param array $attributeNames list of attributes that need to be saved. Defaults to `null`,
297+ * meaning all attributes that are loaded from DB will be saved.
298+ * @return int|false the number of rows affected, or false if validation fails
299+ * or [[beforeSave()]] stops the updating process.
300+ * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
301+ * being updated is outdated.
302+ * @throws \Exception|\Throwable in case update failed.
303+ */
304+ public function update ($ runValidation = true , $ attributeNames = null )
305+ {
306+ if ($ runValidation && !$ this ->validate ($ attributeNames )) {
307+ Yii::info ('Model not updated due to validation error. ' , __METHOD__ );
308+ return false ;
309+ }
310+
311+ if (!$ this ->isTransactional (self ::OP_UPDATE ))
312+ return $ this ->updateInternal ($ attributeNames );
313+
314+ $ result = null ;
315+ static ::getDb ()->transaction (function ()use ($ attributeNames ,&$ result ){
316+ $ result = $ this ->updateInternal ($ attributeNames );
317+ });
318+ return $ result ;
319+ }
320+
246321 /**
247322 * @see ActiveRecord::update()
248323 * @throws StaleObjectException
@@ -314,6 +389,13 @@ public function delete()
314389 $ this ->afterDelete ();
315390 }
316391
392+ if (!$ this ->isTransactional (self ::OP_DELETE ))
393+ return $ this ->deleteInternal ();
394+
395+ $ result = null ;
396+ static ::getDb ()->transaction (function ()use (&$ result ){
397+ $ result = $ this ->deleteInternal ();
398+ });
317399 return $ result ;
318400 }
319401
0 commit comments