简体   繁体   中英

Problem with loading relations in Doctrine ORM

I'm using Doctrine ORM for PHP and I have a question about loading relations. A model seems to load a different set of relations based on how it was loaded, does anyone know a way around this?

That might be a bit confusing. Here's an example. Say we have model A and model B, with a many to many relationship - A has one or more Bs. In my data set I have one A record, "A1", which is related to two B records, "B1" & "B2".

There's two ways I need to load an A record, but in both scenarios I want to be able to get all related B records using $myModelA->B .

The first scenario is by loading "A1" directly. So I'd go $myModelA = A_Table->find("A1") , simple. Then when I go $myModelA->B , I will be able to see both "B1" & "B2". This is expected.

The second scenario causes the problems. I want to find all A records that are related to "B2". So I create a Doctrine_Query and do a join between A and B and specify that I want A records that are related to "B2". Now I want to go through my list of A records, one of which is "A1". So since I have the "A1" model in $myModelA , same as in the first scenario, I expect that when I go $myModelA->B I can see both "B1" & "B2".

But this isn't the case. I only see "B2". I presume this is because the query I used to retrieve "A1" in the first place required only "B2" related records, so then when I follow the relation back the other way that condition is somehow still in place and so I can only get "B2".

Question is…how do to force the model in scenario two to ignore the previous query and load all related records to that model?

Thanks!

我认为您可以在A上调用refresh() 。请确保以true作为参数调用它,以便刷新您的关系。

$A1->refresh(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM