Answer the question
In order to leave comments, you need to log in
How to distribute the code in the model and view?
Recently started to practice writing in Yii2. I have a problem with output of records. All the code that is needed works, but in fact it is only in the model, whether it is possible (necessary) to distribute it in the model and view. Please tell me how to distribute the code so that it works while in the model and the view (if it's right).
Controller
Model
View
Result
If I move part of the code to the controller, then it does not see the variables $posts $posts2 $rows
$rating = RatingGroup::getPosts();
echo '<table class="table table-striped table-bordered">';
echo '<thead>';
echo '<tr>';
echo '<td>#</td>';
echo '<td>Студент</td>';
echo '<td>Ср. балл</td>';
foreach ($posts2 as $post2) {
echo '<td>' . $post2->data . '</td>';
}
echo '</tr>';
echo '</thead>';
/////////////////////
echo '<tbody>';
foreach ($posts AS $post){
foreach ($rows as $key => $avalue) {
foreach ($avalue as $value) {
echo '<td>' . $post->$value . '</td>';
}
}
echo '</tbody>';
}
echo '</table>';
Answer the question
In order to leave comments, you need to log in
It is not necessary to shove a view into the model.
It comes to the controller, from it to the model and from the model to the controller, and from the controller to the view.
controller - for requests
view - for html
model - for data
What you've done is bullshit.
Why is there some html in the model? This is a pure abstraction of working with data, there should not even smell of any html there.
The model knows how to work with data, it doesn’t know about the controller or the view, it stupidly knows how to get it from the database, add it, divide it, remember the result in the properties, save it in the database (well, depending on the AR OR DM).
The controller creates class objects, passes data to them for initiation (most often received from the router), stirs up a couple of transformations with them if necessary, then passes all the data to the view object. All. Can't do anything else. Intermediary.
View - almost pure html (or another type of display, xml or json, not the point), plus logic at the level of forich, for data output. There is practically nothing else.
And you got some kind of vinaigrette. Tomorrow the design has changed - we climb into all models to change the appearance of the site. Just a star.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question