YRM
YRM

Reputation: 301

DQL select association

I have 2 doctrine entity's,

entity A has a field name

entity B has a field city

entity A has a one to many association to entity B named bees

Now i try to select all A entity's that have a name like 'test' or has a association to a B entity that has a city like 'test'

the DQL i have:

SELECT a FROM A a INNER JOIN a.bees b WHERE a.name LIKE ?1 OR b.city LIKE ?1

This works when A has a associated B but if there is no associated B the result is empty even if the name of A matches.

How do i fix this?

Upvotes: 1

Views: 273

Answers (1)

Eloims
Eloims

Reputation: 5224

You should use a left join

SELECT a FROM A a LEFT JOIN a.bees b WHERE a.name LIKE ?1 OR b.city LIKE ?1

Here is a cheat sheet: http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg

Upvotes: 2

Related Questions