Skip to content

Instantly share code, notes, and snippets.

@m8rge
Created January 19, 2015 12:52
Show Gist options
  • Save m8rge/ac0145d5dac3f1c6e032 to your computer and use it in GitHub Desktop.
Save m8rge/ac0145d5dac3f1c6e032 to your computer and use it in GitHub Desktop.
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