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

编程入门 行业动态 更新时间:2024-10-26 20:25:06
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']))]);

更多推荐

本文发布于:2023-08-07 15:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   逻辑   模型   model   view

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!