I
I
Ivan Petrov2017-06-13 19:14:14
Yii
Ivan Petrov, 2017-06-13 19:14:14

How to properly organize relationships between tables?

Hello. I got tired with the table links in Yii 1. I can't get the data correctly in any way.
I have 2 tables:
1 table of warehouses, there are id, name, city_id
And in fact, using the city_id field, I should get the name of the city from the table with cities.
I organize communication in the City model

'stockInCity' => array(self::HAS_MANY, 'Stock', 'city_id'),

After I call in the component
$a = City::model()->with('stockInCity')->findByPk(1250);
        echo "<pre>"; print_r($a); echo "</pre>";

At the output I get
673814bebcd8423e8bfac9a7418fabd8.png
where the city_id in my case is Moscow.
It is necessary, as I understand it, to turn on the contrary to the warehouses, but to stockInCity, so that there is an array of the city. Swapping works. Tried to make a connection in the Stock model
'stockInCity' => array(self::HAS_MANY, 'City', 'id'),
but the output is an empty object.
I'm definitely doing something wrong, but I can't figure it out. What could be the problem? Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2017-06-13
@r0hack

If you need to get City from Stock, then you need to specify in Stock
PS It is more correct to call not "stockInCity", but "stocks"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question