When would it be worth it to maintain an inverse relationship in Doctrine2?

Viewed 1016

In the Doctrine manual, under Constrain relationships as much as possible, it gives the advice "Eliminate nonessential associations" and "avoid bidirectional associations if possible". I don't understand what criteria would make an association "essential".

I say this because it seems that you would often want to go from the One side of a One-to-Many association rather than from the Many side. For example, I would want to get all of a User's active PhoneNumbers, rather than get all active PhoneNumbers and their associated User. This becomes more important when you have to traverse multiple One-to-Many relations, e.g. if you wanted to see all Users with a MissedCall from the last two days (MissedCall->PhoneNumber->User).

This is how the simple case would look with an inverse association:

SELECT * FROM User u
LEFT JOIN u.PhoneNumbers p WITH p.active

It would make it more sensible if there were a way to go across a given relation in the opposite direction in DQL, like the following raw SQL:

SELECT * FROM User u
LEFT JOIN PhoneNumber p ON p.User_id = u.id AND p.active

Can someone explain why they give this advice, and in what cases it would be worth ignoring?

-- Edit --

If there are mitigating factors or other workarounds, please give me simple example code or a link.

I do not see any way to traverse a relation's inverse when that inverse is not defined, so I'm going to assume that building custom DQL is not in fact a solution -- there are some joins that are trivial with SQL that are impossible with DQL, and hydration probably wouldn't work anyway. This is why I don't understand why adding inverse relations is a bad idea.

2 Answers
Related