Answer the question
In order to leave comments, you need to log in
How to make it easier to work with relations in Yii?
There are two tables linked by pk. I select the data from one table together with the data from the second on BELONGS_TO. In the second table, I need only one field, but I have to display the information like this: $model->%relationName%->%attributeName
% %?
Answer the question
In order to leave comments, you need to log in
In the main model, write a method:
public function get%attributeName%(){
return $this->%relationName%->%attributeName%;
}
$model->get%attributeName%(); // вариант 1
$model->%attributeName%; // вариант 2
Maybe it makes sense to denormalize the table then? And so this is quite normal. Or write a getter. But you shouldn’t override magic methods - not only are getters better suited for this, but people after you on the project see this kind of shit code and get upset.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question