Skip to content

Commit 2f51d96

Browse files
committed
Delete method added to the trait
1 parent 92f2a6a commit 2f51d96

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/ActiveRecordInheritanceTrait.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212
use yii\base\UnknownPropertyException;
1313
use yii\base\UnknownMethodException;
14-
use yii\db\Exception;
14+
use yii\db\Exception as DbException;
1515
use yii\base\Exception as BaseException;
1616
use Yii;
17+
use Exception;
1718

1819
/**
1920
* Trait to simulate inheritance between two ActiveRecordInterface classes.
@@ -207,7 +208,7 @@ public function getAttributes($names = null, $except = array()) {
207208
* trait will be lost.
208209
*
209210
* @return boolean
210-
* @throws Exception
211+
* @throws \Exception
211212
*/
212213
public function save($runValidation = true, $attributeNames = null) {
213214
if ($runValidation === true && $this->validate($attributeNames) === false) {
@@ -218,12 +219,12 @@ public function save($runValidation = true, $attributeNames = null) {
218219
$trans = static::getDb()->beginTransaction();
219220
try {
220221
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');
222223
}
223224

224225
$this->{$this->parentAttribute()} = $this->_parent()->{$this->parentPrimaryKey()};
225226
if (parent::save(false, $attributeNames) === false) {
226-
throw new Exception('Unable to save current model');
227+
throw new DbException('Unable to save current model');
227228
}
228229
$trans->commit();
229230
return true;
@@ -233,6 +234,43 @@ public function save($runValidation = true, $attributeNames = null) {
233234
}
234235
}
235236

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+
236274
/**
237275
* Validates the parent and the current model.
238276
* DO NOT OVERRIDE THIS METHOD ON TRAIT USER CLASS or functionality of this

0 commit comments

Comments
 (0)