U
U
Ultraice2016-08-05 15:37:54
Yii
Ultraice, 2016-08-05 15:37:54

How to call a model method from a view?

Question about yii2. In the controller, I changed the data format from an object to an array of asArray() functions.
$games = $games->asArray()->all();
After that, respectively, the methods of the model that were called in the form ceased to work. Because the array comes.

foreach ($games as $key => $game){
    echo $game->pretty_date($game->date_add]);
}

Are there other ways? As an option, make the methods static and connect the model class in the form or transfer the methods to the controller, but it seems to me that everything should be simpler)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-08-05
@qonand

1. You cannot use methods because you don't have a class you have an array, but there are no methods in it
2. For what purpose do you use an array? Do you select hundreds of thousands of records? Or what is your purpose for using it?
3. If you are already working with an array, then the $game->date_add construct does not make sense, because it's not an object, it can't have properties. You need to work with the array as $game['date_add']
4. Judging by the code, the pretty_date method formats the date in the form you need. That's just the question - why? if there is a built-in formatting mechanism - you can read more about it here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question