A
A
Artur Fazylov2017-04-22 11:19:10
Yii
Artur Fazylov, 2017-04-22 11:19:10

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
540dd657271a4c2da39fe25f19e56376.PNG
Model
03457e92452f4d95b7f9997925320152.PNG
View
83a8f7168faa4d6daef9272bd07a5f8f.PNG
Result
410256b43a514643a77a50c989a0af43.PNG
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

3 answer(s)
V
Viktor Yanyshev, 2017-04-22
@villiwalla

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.

M
Maxim Timofeev, 2017-04-22
@webinar

controller - for requests
view - for html
model - for data
What you've done is bullshit.

T
ThunderCat, 2017-04-22
@ThunderCat

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 question

Ask a Question

731 491 924 answers to any question