1

Sure I'm missing out something or not thinking about a better way to accomplish what I need, but here is what I want to do.

I have three models: A, B and C. A has a relation MANY-MANY with B called relation1, and Bhas a relation MANY-MANY with C, called relation2. I want to get all B records with their C relations (eager loading), but just from an A instance (lazy loading, because I don't want to waste memory loading all relation1 instances).

So, what I want to do should be something like the following:

A::model()->findByPk($somePrimaryKey)->relation1->with('relation2');

Of course that's not working because using relation1 lazy loading I'm getting an array of instances and then I'm tryng to apply the with() method to a non-object.

Sure I can do a little bit of work around iterating through the array, but I want to know if there is a quick and elegent way :)

Thank you!

1 Answer 1

3

You can try this:

A::model()->findByPk($somePrimaryKey)->with('relation1.relation2');

I cant find any sense in that you "don't want to waste memory loading all relation1 instances"

This way you are just loading the B models that relate to that specific A model. not all of them.

Also take a look at Relational Query with through

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, thank you @Asgaroth. I thought that nesting the two with that way I was loading first all records of relation1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.