Doctrine DQL with multiple joined tables
Asked Answered
J

2

6

i have entities **target,citytarget,city,cityplace,place

citytarget & cityplace are pivot tables that connect t&c and c&p

i need the places for given city name and a target id

i tried the following DQL:

SELECT t,c,p FROM MulticatorBundle:Target t
            join t.cities ct
            join ct.cities c
            join c.places cp
            join cp.places p
            where c.name like '%Stahmeln%'

But i receive:

result: The parent object of entity result with alias 'c' was not found. The parent alias is 'ct'.

i dont know any further....

a plain SQL could be:

select * from target 
left join citytarget on citytarget.target_id = target.id
left join city on citytarget.city_id = city.id 
left join cityplace on cityplace.city_id = city.id 
left join place on cityplace.id = place.id 
where target.id = 1 
and city.name like \'%Stahmeln%\'

Adrian

Jentoft answered 12/2, 2016 at 15:52 Comment(0)
R
3

You need to do

SELECT t,ct, c, cp ,p FROM MulticatorBundle:Target t
            join t.cities ct
            join ct.cities c
            join c.places cp
            join cp.places p
            where c.name like '%Stahmeln%'

Hope this help you

Rollback answered 12/2, 2016 at 15:57 Comment(2)
hi abdiel. tx for you answer.,i receive the notice 'undefined index city' now ;/Jentoft
fixed it. it was a problem with plural and singular in entity variablesJentoft
E
11

You always need your parent entity in your select. In this case:

SELECT ct, c, cp, t, p

is needed

Empedocles answered 12/2, 2016 at 16:45 Comment(1)
Thank you! This was giving me a headache.Estaminet
R
3

You need to do

SELECT t,ct, c, cp ,p FROM MulticatorBundle:Target t
            join t.cities ct
            join ct.cities c
            join c.places cp
            join cp.places p
            where c.name like '%Stahmeln%'

Hope this help you

Rollback answered 12/2, 2016 at 15:57 Comment(2)
hi abdiel. tx for you answer.,i receive the notice 'undefined index city' now ;/Jentoft
fixed it. it was a problem with plural and singular in entity variablesJentoft

© 2022 - 2024 — McMap. All rights reserved.