Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Yii2 Softdelete trait (trait better than behavior)
<?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();
}
}
<?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
You can’t perform that action at this time.