Yii2从视图到模型的逻辑转移(Yii2 transfer logic from view to model)

我想安排我的代码更好一点。 我有这个看法,由giiant生成:

$form->field($model, 'land_id')->dropDownList(ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id'])),]);

有人在这里告诉我,在stackoverflow,这不是很好。 所以我想转移这部分:

ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'

进入模型。 这同样适用于网格过滤器下拉菜单:

'filter' => ArrayHelper::map(\app\models\base\Land::find()->asArray()->all(), 'id', 'name'),

是否有意义? 我想是的,我希望如此。 我试图以模型2的方式实现它:

public function getAllAsArray() { return ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'); }

要么

public function getAllAsArray() { return ArrayHelper::map($this->find()->orderBy('name')->all(), 'id', 'name'); }

我想从View / Grid(Zip - Land)中调用它:

'filter' => $model->land->allAsArray,

但我收到以下错误:未定义的变量:模型

然后尝试这种方式:

'filter' => function ($model) {$model->getLand()->one()->getAllAsArray();}, 'filter' => function ($model) {$model->getLand()->getAllAsArray();},

那么我不会收到错误消息,但它也不起作用。

并以表格(Zip - Land)的形式显示:

$form->field($model, 'land_id')->dropDownList($model->land->allAsArray(), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id'])),]);

但我收到以下错误:调用成员函数getAllAsArray()null

你能指点我正确的方向吗? 我想我基本不了解某些事情,这让我感到困惑。 非常感谢您提前!

I would like to arrange my code a little bit better. I have this in view, generated by giiant:

$form->field($model, 'land_id')->dropDownList(ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id'])),]);

Somebody has told me here on stackoverflow that it's not really nice. So I would like to transfer this part:

ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'

into model. The same applies to grid filter dropdowns:

'filter' => ArrayHelper::map(\app\models\base\Land::find()->asArray()->all(), 'id', 'name'),

Does it make sense? I think so, I hope so. I've tried to implement it in model Land 2 ways:

public function getAllAsArray() { return ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'); }

or

public function getAllAsArray() { return ArrayHelper::map($this->find()->orderBy('name')->all(), 'id', 'name'); }

and I wanted to call it from View/Grid (Zip - Land):

'filter' => $model->land->allAsArray,

but I'm getting the following error: Undefined variable: model

then tried this way:

'filter' => function ($model) {$model->getLand()->one()->getAllAsArray();}, 'filter' => function ($model) {$model->getLand()->getAllAsArray();},

then I get no error messages, but it's also not working.

and in Form (Zip - Land) the same way:

$form->field($model, 'land_id')->dropDownList($model->land->allAsArray(), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id'])),]);

but I'm getting the following error: Call to a member function getAllAsArray() on null

Can you please point me to the right direction? I think I don't understand something basically and this disturbes me. Thank you very much in advance!

最满意答案

土地模型

public static function getAllAsArray() { return ArrayHelper::map(Land::find()->orderBy('name')->all(), 'id', 'name'); }

指数

use app\models\Land; 'filter' => Land::getAllAsArray(),

形成

use app\models\Land; $form->field($model, 'land_id')->dropDownList(Land::getAllAsArray(), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id']))]);

Land model

public static function getAllAsArray() { return ArrayHelper::map(Land::find()->orderBy('name')->all(), 'id', 'name'); }

index

use app\models\Land; 'filter' => Land::getAllAsArray(),

form

use app\models\Land; $form->field($model, 'land_id')->dropDownList(Land::getAllAsArray(), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id']))]);Yii2从视图到模型的逻辑转移(Yii2 transfer logic from view to model)

我想安排我的代码更好一点。 我有这个看法,由giiant生成:

$form->field($model, 'land_id')->dropDownList(ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id'])),]);

有人在这里告诉我,在stackoverflow,这不是很好。 所以我想转移这部分:

ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'

进入模型。 这同样适用于网格过滤器下拉菜单:

'filter' => ArrayHelper::map(\app\models\base\Land::find()->asArray()->all(), 'id', 'name'),

是否有意义? 我想是的,我希望如此。 我试图以模型2的方式实现它:

public function getAllAsArray() { return ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'); }

要么

public function getAllAsArray() { return ArrayHelper::map($this->find()->orderBy('name')->all(), 'id', 'name'); }

我想从View / Grid(Zip - Land)中调用它:

'filter' => $model->land->allAsArray,

但我收到以下错误:未定义的变量:模型

然后尝试这种方式:

'filter' => function ($model) {$model->getLand()->one()->getAllAsArray();}, 'filter' => function ($model) {$model->getLand()->getAllAsArray();},

那么我不会收到错误消息,但它也不起作用。

并以表格(Zip - Land)的形式显示:

$form->field($model, 'land_id')->dropDownList($model->land->allAsArray(), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id'])),]);

但我收到以下错误:调用成员函数getAllAsArray()null

你能指点我正确的方向吗? 我想我基本不了解某些事情,这让我感到困惑。 非常感谢您提前!

I would like to arrange my code a little bit better. I have this in view, generated by giiant:

$form->field($model, 'land_id')->dropDownList(ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id'])),]);

Somebody has told me here on stackoverflow that it's not really nice. So I would like to transfer this part:

ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'

into model. The same applies to grid filter dropdowns:

'filter' => ArrayHelper::map(\app\models\base\Land::find()->asArray()->all(), 'id', 'name'),

Does it make sense? I think so, I hope so. I've tried to implement it in model Land 2 ways:

public function getAllAsArray() { return ArrayHelper::map(app\models\Land::find()->orderBy('name')->all(), 'id', 'name'); }

or

public function getAllAsArray() { return ArrayHelper::map($this->find()->orderBy('name')->all(), 'id', 'name'); }

and I wanted to call it from View/Grid (Zip - Land):

'filter' => $model->land->allAsArray,

but I'm getting the following error: Undefined variable: model

then tried this way:

'filter' => function ($model) {$model->getLand()->one()->getAllAsArray();}, 'filter' => function ($model) {$model->getLand()->getAllAsArray();},

then I get no error messages, but it's also not working.

and in Form (Zip - Land) the same way:

$form->field($model, 'land_id')->dropDownList($model->land->allAsArray(), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id'])),]);

but I'm getting the following error: Call to a member function getAllAsArray() on null

Can you please point me to the right direction? I think I don't understand something basically and this disturbes me. Thank you very much in advance!

最满意答案

土地模型

public static function getAllAsArray() { return ArrayHelper::map(Land::find()->orderBy('name')->all(), 'id', 'name'); }

指数

use app\models\Land; 'filter' => Land::getAllAsArray(),

形成

use app\models\Land; $form->field($model, 'land_id')->dropDownList(Land::getAllAsArray(), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id']))]);

Land model

public static function getAllAsArray() { return ArrayHelper::map(Land::find()->orderBy('name')->all(), 'id', 'name'); }

index

use app\models\Land; 'filter' => Land::getAllAsArray(),

form

use app\models\Land; $form->field($model, 'land_id')->dropDownList(Land::getAllAsArray(), [ 'prompt' => Yii::t('app', 'Select'), 'disabled' => (isset($relAttributes) && isset($relAttributes['land_id']))]);