Created
January 19, 2015 12:52
-
-
Save m8rge/ac0145d5dac3f1c6e032 to your computer and use it in GitHub Desktop.
Yii2 Softdelete trait (trait better than behavior)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait SoftDeleteTrait | |
{ | |
public static function deletedProperties() | |
{ | |
return ['deleted_at' => time()]; | |
} | |
public static function deleteAll($condition = '', $params = []) | |
{ | |
$command = static::getDb()->createCommand(); | |
$command->update(static::tableName(), static::deletedProperties(), $condition, $params); | |
return $command->execute(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User extends ActiveRecord | |
{ | |
use SoftDeleteTrait; | |
public static function deletedProperties() // we can override deleted properties | |
{ | |
return ['deleted' => 1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment