David J Eddy
David J Eddy

Reputation: 2037

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

Upvotes: 2

Views: 1231

Answers (2)

soju
soju

Reputation: 25322

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().

Upvotes: 1

s_mart
s_mart

Reputation: 765

I think you should use Customized Query Class

It is already exist in library, what you are using - here github

If you want - you can also re-define find method.

Upvotes: 0

Related Questions