Home  >  Article  >  Backend Development  >  yii2模版里的匿名函数问题

yii2模版里的匿名函数问题

WBOY
WBOYOriginal
2016-06-06 20:32:031097browse

控制器里没有$model变量

<code>public function actionIndex()
    {
        // if (!ctype_digit($parent)) {
        //     throw new InvalidValueException();
        // }
        $searchModel = new NsortSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            // 'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
</code>

但视图中的$model是从哪里来的

<code> [
      'attribute' => 'parent',
      'value' => function ($model) {
                    return empty($model->parent) ? '-' : $model->parent->name;
                },
            ],
</code>

回复内容:

控制器里没有$model变量

<code>public function actionIndex()
    {
        // if (!ctype_digit($parent)) {
        //     throw new InvalidValueException();
        // }
        $searchModel = new NsortSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            // 'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
</code>

但视图中的$model是从哪里来的

<code> [
      'attribute' => 'parent',
      'value' => function ($model) {
                    return empty($model->parent) ? '-' : $model->parent->name;
                },
            ],
</code>

这是在 GridView里面的,

简单来说就是你通过的 dataProvider 通用 getModels() 获取所有的Model, 然后遍历生成表格,

每一行就是一个 Model, 在内容如果 value 是匿名方法的话就会调用 call_user_func($this->value, $model, ..)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn