Answer the question
In order to leave comments, you need to log in
Database query in model or controller?
Help, I'm completely confused.
Here is a controller with an IndexController action that has a Page model.
Here it is necessary to display a certain value in the index, for example with name="TestPage" , then it is necessary to refer to the database in the controller?
public function IndexController() {
$page= Page::find()->where(['name'=>'TestPage']);
return $this->render('index', ['page'=>$page]);
}
or, in some cases, met a request to the database in the view. The piece is taken from yii starter kit
[
'attribute'=>'category_id',
'value'=>function ($model) {
return $model->category ? $model->category-> title : null
},
'filter'=>\yii\helpers\ArrayHelper::map(\common\models\ArticleCategory::find()->all(), 'id', 'title')
],
is that correct?
Answer the question
In order to leave comments, you need to log in
In the controller in your code (slightly unfinished, in my opinion) you are not accessing the database directly, but the model - this is what MVC implies. BUT! The example is too simple and in this particular case everything is according to the canon, but how much you understand this correctly is another question.
It is dangerous to play with models in view, it is advisable to pass an object ready for display there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question