php - Yii2 - 如何设置 Find() 之后的行为

标签 php yii yii2

型号:

....

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/

相关文章:

mysql - 在 yii2 中按 rand() 排序

css - yii2 css 文件不会加载,因为它是 MIME 类型 text/html 在 windows7 apache 服务器上不是 text/css

php 从 2 个 mysql 查询返回 1 个数据集

php - 字符在 UTF-8 网站上显示不正确

javascript - TreeView 留言板 - 禁用对 child 不起作用的提交按钮

php - 属性名称必须仅在 C :\xampp\htdocs\sample\vendor\yiisoft\yii2\helpers\BaseHtml. php 中包含单词字符

java - 复合模式中的多叶方法问题

php - curl 错误 : Recv failure: Connection reset by peer - PHP Curl

javascript - 在 Controller 操作中调用 Javascript 函数 -YII

php - Yii addInCondition 与 float : How to ? 为什么 addInCondition ('column' , array(1.1, 1.3)) 不工作?