型号:
....
public $yourWay;
....
public function afterFind(){
parent::afterFind();
// echo $this->yourWay (Nothing display)
//something like:
switch($this->yourWay){
//Do something
case "abc": break;
//Do something
case "cde": break;
}
}
Controller :
$model = new Model();
$model->yourWay = 'oneway';
$dt = $model::find()->one();
我尝试使用 $model->scenario = "abc"
和 echo $this->scenario
,但它不起作用。
我是 Yii2 新用户,所以希望有人能帮助我...
最佳答案
型号 afterFind()
不是$model
您正在调用find()
在。它是仅由数据库数据填充的新对象。您必须使用静态变量将任何内容传递给 afterFind()
。请记住静态变量的性质,并在完成搜索后将其设置为 null,这样就不会影响以后的搜索。
型号:
...
public static $yourWay;
...
public function afterFind(){
parent::afterFind();
switch(self::$yourWay){
case "abc":
break;
case "cde":
break;
}
}
Controller :
$model = new Model();
$model::$yourWay = 'oneway';
$dt = $model::find()->one();
$model::$yourWay = null;
关于php - Yii2 - 如何设置 Find() 之后的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43451094/