I
I
Ilya Plotnikov2013-12-25 13:45:44
Yii
Ilya Plotnikov, 2013-12-25 13:45:44

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

2 answer(s)
E
enchikiben, 2013-12-25
@ilyaplot

In the main model, write a method:

public function get%attributeName%(){
   return $this->%relationName%->%attributeName%;
}

call:
$model->get%attributeName%(); // вариант 1
$model->%attributeName%; // вариант 2

you can also override the yii magic method __get

S
Sergey, 2013-12-25
Protko @Fesor

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 question

Ask a Question

731 491 924 answers to any question