Search code examples
phpactiverecordyii2soft-delete

Yii2, Softdelete, self::tableName(), and ignoring `deleted` records


I have a base model class that contains the SoftDelete behavior (cornernote/yii2-softdelete). When a record is deleted the column in the table deleted_at is populated with a timestamp.

Attempted to override the find() method in the base model but self::className() does not return the table prefix with the name.

return parent::find()->where([self::tableName() . '.deleted_at' => null]);

I have to add it to each model class in order to get the proper (full) table name.

The ask: How best to ignore records in a table that have a column populated. The solution has to work when the model is accessed for ActiveDataProvider([...]), find()->...one(), and find()->...all() situations.

TIA


Solution

  • You should read this about Late Static Bindings :

    http://php.net/manual/en/language.oop5.late-static-bindings.php

    Late static bindings introduce the static keyword that references the class that was initially called at runtime

    So, you should simply use static::tableName() instead of self::tableName().