首页 > yii2模版里的匿名函数问题

yii2模版里的匿名函数问题

控制器里没有$model变量

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,
        ]);
    }

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

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

这是在 GridView里面的,

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

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

【热门文章】
【热门文章】