Yii2、Softdelete、self::tableName() 和忽略“已删除”记录
Yii2, Softdelete, self::tableName(), and ignoring `deleted` records
我有一个包含 SoftDelete 行为 (cornernote/yii2-softdelete
) 的基础模型 class。删除记录时,table deleted_at
中的列会填充时间戳。
试图覆盖 base
模型中的 find() 方法,但 self::className() 没有 return table 前缀的名称。
return parent::find()->where([self::tableName() . '.deleted_at' => null]);
我必须将它添加到每个模型 class 以获得正确的(完整的)table 名称。
问题:如何最好地忽略 table 中填充了列的记录。当针对 ActiveDataProvider([...])、find()->...one() 和 find()->...all() 情况访问模型时,解决方案必须有效。
TIA
我认为你应该使用 Customized Query Class
它已经存在于库中,你正在使用什么 - here github
如果需要 - 您也可以重新定义 find
方法。
您应该阅读有关后期静态绑定的这篇文章:
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
因此,您应该简单地使用 static::tableName()
而不是 self::tableName()
。
我有一个包含 SoftDelete 行为 (cornernote/yii2-softdelete
) 的基础模型 class。删除记录时,table deleted_at
中的列会填充时间戳。
试图覆盖 base
模型中的 find() 方法,但 self::className() 没有 return table 前缀的名称。
return parent::find()->where([self::tableName() . '.deleted_at' => null]);
我必须将它添加到每个模型 class 以获得正确的(完整的)table 名称。
问题:如何最好地忽略 table 中填充了列的记录。当针对 ActiveDataProvider([...])、find()->...one() 和 find()->...all() 情况访问模型时,解决方案必须有效。
TIA
我认为你应该使用 Customized Query Class
它已经存在于库中,你正在使用什么 - here github
如果需要 - 您也可以重新定义 find
方法。
您应该阅读有关后期静态绑定的这篇文章:
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
因此,您应该简单地使用 static::tableName()
而不是 self::tableName()
。