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; }, ],
回复内容: 控制器里没有$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, ..)
