应该改变数据在模型中还是在视图中被看到?(php - yii)


Should changing how data is seen be in the model or the view? (PHP - YII)

我最近在MVC实践中读到,几乎所有与查询相关的数据都应该在模型中。但是,我感兴趣的是,当您使用外键更改应该在模型或视图中显示的查询对象的显示名称时。

这是我正在使用的例子。

     // the id is being saved from the model 
$callfood=Food::model()->getfoodlist();
     // in this part $callfood is using a foreign key to display the 'food_id' as the   names of the food 
     echo $form->dropDownList($model, 'food_id',$callfood); ?>

对我来说,dropDownList代码应该在视图中是有意义的,但我确实觉得我正在改变它在下拉列表中显示/保存的方式。这应该继续存在吗?或者应该将其推入模型,以便在我想更改视图并保留下拉列表时可以重用它?

这取决于你的工作流程是如何设置的。但基本上你把模型保存在一个变量中然后发送给视图。然后你可以使用dropDownList($model, 'food_id',$f_model->getfoodlist())。我建议你仔细看看PHP和Yii是如何工作的;)