11
11
12
12
use yii \base \UnknownPropertyException ;
13
13
use yii \base \UnknownMethodException ;
14
- use yii \db \Exception ;
14
+ use yii \db \Exception as DbException ;
15
15
use yii \base \Exception as BaseException ;
16
16
use Yii ;
17
+ use Exception ;
17
18
18
19
/**
19
20
* Trait to simulate inheritance between two ActiveRecordInterface classes.
@@ -207,7 +208,7 @@ public function getAttributes($names = null, $except = array()) {
207
208
* trait will be lost.
208
209
*
209
210
* @return boolean
210
- * @throws Exception
211
+ * @throws \ Exception
211
212
*/
212
213
public function save ($ runValidation = true , $ attributeNames = null ) {
213
214
if ($ runValidation === true && $ this ->validate ($ attributeNames ) === false ) {
@@ -218,12 +219,12 @@ public function save($runValidation = true, $attributeNames = null) {
218
219
$ trans = static ::getDb ()->beginTransaction ();
219
220
try {
220
221
if ($ this ->_parent ()->save (false , $ attributeNames ) === false ) {
221
- throw new Exception ('Unable to save parent model ' );
222
+ throw new DbException ('Unable to save parent model ' );
222
223
}
223
224
224
225
$ this ->{$ this ->parentAttribute ()} = $ this ->_parent ()->{$ this ->parentPrimaryKey ()};
225
226
if (parent ::save (false , $ attributeNames ) === false ) {
226
- throw new Exception ('Unable to save current model ' );
227
+ throw new DbException ('Unable to save current model ' );
227
228
}
228
229
$ trans ->commit ();
229
230
return true ;
@@ -233,6 +234,43 @@ public function save($runValidation = true, $attributeNames = null) {
233
234
}
234
235
}
235
236
237
+ /**
238
+ * Deletes the table row corresponding to this active record.
239
+ *
240
+ * This method performs the following steps in order:
241
+ *
242
+ * 1. call [[beforeDelete()]]. If the method returns false, it will skip the
243
+ * rest of the steps;
244
+ * 2. delete the record from the database;
245
+ * 3. call [[afterDelete()]].
246
+ *
247
+ * In the above step 1 and 3, events named [[EVENT_BEFORE_DELETE]] and [[EVENT_AFTER_DELETE]]
248
+ * will be raised by the corresponding methods.
249
+ *
250
+ * @return integer|false the number of rows deleted, or false if the deletion is unsuccessful for some reason.
251
+ * Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
252
+ * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
253
+ * being deleted is outdated.
254
+ * @throws \Exception in case delete failed.
255
+ */
256
+ public function delete () {
257
+ $ trans = static ::getDb ()->beginTransaction ();
258
+ try {
259
+ if (parent ::delete () === false ) {
260
+ throw new DbException ('Unable to delete current model ' );
261
+ }
262
+ $ result = $ this ->_parent ()->delete ();
263
+ if ($ result === false ) {
264
+ throw new DbException ('Unable to delete parent model ' );
265
+ }
266
+ $ trans ->commit ();
267
+ return $ result ;
268
+ } catch (Exception $ e ) {
269
+ $ trans ->rollback ();
270
+ throw $ e ;
271
+ }
272
+ }
273
+
236
274
/**
237
275
* Validates the parent and the current model.
238
276
* DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this
0 commit comments