5

A little bit connecting to my previous question:

I have following tables/models: enter image description here

I've managed to join all tables for actionIndex, but I would like to implement now the same thing for actionView, but it seems find() and findOne() doesn't work the same. Joins don't work with findModel($id)

I don't really have clue, can you please point me to the right direction? In fact I need only to show related data of model A in model BCD view, and I'm quite sure there is a simple way doing it, but I can't find anything, I don't even really know what to look for. Becuse the problem is, with normal relationships I can only reach out to table B, so maximum 2 levels. I've tried to create a relationship that reaches out to 3rd level but it's not working. Thanks.

Community
  • 1
  • 1
user2511599
  • 796
  • 1
  • 13
  • 38

1 Answers1

6

You do not have to define multi levels relations.

If you have to do this for 1 record in BCD you can just access it with

$BCDmodel->BC->B->A->attribute

of course use the names of the relations that you have defined.

This will also work when showing this info in a table BUT ... it is quite inefficient. For every row you show you will get a lot of queries so you should change the query to make it more efficient.

$query = BCD::find()->with(['BC', 'BC.B', 'BC.B.A']).....

This will join everything together and make the query a much better one, when you do need to show the data you can still use

$BCDmodel->BC->B->A->attribute

Just make sure that your BCD model has a relation to BC named BC, your BC model has a relation to B called B, your B model has a relation to A called A and the above should work.

Mihai P.
  • 9,307
  • 3
  • 38
  • 49
  • It works for index.php (it doesn't join everything together, at least not on sql level: there are 6 standalone queries in debugger), but still, in **actionView** it doesn't work. Maybe because only of something little, but as soon as I change in findModel _findOne($id)_ to _find($id)_, it stops working. Getting unknown property: yii\db\ActiveQuery::BCD – user2511599 Jul 07 '15 at 20:34
  • Why would find($id) work? http://www.yiiframework.com/doc-2.0/yii-db-activerecord.html#find()-detail it is not suppose to accept a parameter. You can use $customer = Customer::find()->where(['id' => 1])->one(); – Mihai P. Jul 07 '15 at 22:18
  • 1
    Also yes, it does not create 1 query if you use "$BCDmodel->BC->B->A->attribute" if you want to join everything togheter you have to use BCD::find()->with(['BC', 'BC.B', 'BC.B.A'])..... – Mihai P. Jul 07 '15 at 22:19
  • Yes, okay, $customer = Customer::find()->where(['id' => 1])->one(); works, but BCD::find()->with(['BC', 'BC.B', 'BC.B.A']) alone generates 4 standalone queries (without printing out any data). But that's no problem, I can still use the joinWith function (for actionIndex, where it has the most importance) and that works 100% prefectly (and it's one joined query). I can still use $BCDmodel->BC->B->A->attribute in actionView, where it results not too big load on the system. Thanks! – user2511599 Jul 08 '15 at 19:42