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

yii2模版里的匿名函数问题

WBOY
Release: 2016-06-06 20:32:03
Original
1161 people have browsed it

控制器里没有$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>
Copy after login
Copy after login

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

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

回复内容:

控制器里没有$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>
Copy after login
Copy after login

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

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

这是在 GridView里面的,

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template