Skip to content

SoftDeleteTrait ​

Type
Trait
Namespace
craft\db
Implemented by
craft\records\CategoryGroup, craft\records\EntryType, craft\records\FieldGroup, craft\records\FieldLayout, craft\records\Section, craft\records\Site, craft\records\SiteGroup, craft\records\Structure, craft\records\TagGroup, craft\records\Volume
Since
3.1.0

Soft-delete trait.

This should be implemented by Active Record classes that wish to support soft deletes. With it, Active Query objects returned by yii\db\ActiveRecord::find() will exclude any soft-deleted rows.

The database table should be created with a dateDeleted column (type datetime null).

php
'dateDeleted' => $this->dateTime()->null()

To fetch all rows, including soft-deleted ones, call findWithTrashed() instead of find().

php
$records = MyActiveRecord::findWithTrashed()->all();

To fetch only soft-deleted rows, call findTrashed() instead of find().

php
$records = MyActiveRecord::findTrashed()->all();

Active Record classes that use this trait and also have their own behaviors should rename this trait’s behaviors() method when using the trait, and then call it from the behaviors() method.

php
use SoftDeleteTrait {
    behaviors as softDeleteBehaviors;
}

public function behaviors(): array
{
    $behaviors = $this->softDeleteBehaviors();
    $behaviors['myBehavior'] = MyBehavior::class;
    return $behaviors;
}

Active Record classes that implement a custom find() method will need to manually add a condition to exclude soft-deleted rows.

php
public static function find(): ElementQueryInterface
{
    // @var MyActiveQuery $query
    $query = Craft::createObject(MyActiveQuery::class, [static::class]);
    $query->where(['dateDeleted' => null]);
    return $query;
}

View source

Public Properties ​

PropertyDescription
dateDeletedstring, null – Date deleted

dateDeleted ​

Type
string, null
Default value
null

Date deleted

View source

Public Methods ​

MethodDescription
beforeRestore()This method is called at the beginning of restoring a record.
behaviors()Returns a list of behaviors that this component should behave as.
find()
findTrashed()
findWithTrashed()

beforeRestore() ​

This method is called at the beginning of restoring a record.

View source

behaviors() ​

Since
3.4.0

Returns a list of behaviors that this component should behave as.

Child classes may override this method to specify the behaviors they want to behave as.

The return value of this method should be an array of behavior objects or configurations indexed by behavior names. A behavior configuration can be either a string specifying the behavior class or an array of the following structure:

php
'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]

Note that a behavior class must extend from \craft\db\Behavior. Behaviors can be attached using a name or anonymously. When a name is used as the array key, using this name, the behavior can later be retrieved using \craft\db\getBehavior() or be detached using \craft\db\detachBehavior(). Anonymous behaviors can not be retrieved or detached.

Behaviors declared in this method will be attached to the component automatically (on demand).

View source

Returns ​

array – The behavior configurations.

find() ​

View source

Returns ​

craft\db\ActiveQuery

findTrashed() ​

View source

Returns ​

craft\db\ActiveQuery

findWithTrashed() ​

View source

Returns ​

craft\db\ActiveQuery