Pregunta

My DQL query returns only the FROM object, which is nice if the other object were related, but it isn't.

My Query:

$query = $this->em->createQuery('SELECT c, s FROM MyBundle:Person c, MyBundle:Spot s 
JOIN s.geo_data g JOIN g.features f WHERE f.active = true AND 
ST_Distance(f.location, c.location) < :distance GROUP BY c, s');

This works perfectly in SQL, giving me all the spots and all the persons within :distance of them. But in DQL, it only returns the person object, and since on the database level they are not related, I have no way to fetch the correct spot.

My database setup is correct, I'm using a PostGIS backend and spots and persons are not related in any way. They just happen to be on the same map and I'm querying for spatial relationships.

According to documentation, it's intended behaviour, from what I read, s is being hydrated, but not returned anywhere at all, good job!

How can I teach DQL to please return me what I told it in SELECT? Where's the "I mean what I say, stop being a smartass" switch?

¿Fue útil?

Solución

Doctrine cannot give you both entities if they are not related because if the were related you would get the first entity c where you could get s through the relation.

What you can try is selecting all fields of both entities like

SELECT c.location, ...,  s.geo_data, ...

This will give you an array for each column that contains all fields from both entities. Maybe you can use result set mapping to get the entities if desired.

Otros consejos

If you want to stuck with Doctrine, you HAVE TO define a OneToMany relation between places and people. In this way, you could set up the PeopleRepository and set up a method like getPeopleByLocationAndMaxDistance(Location $location, $distance)

SELECT p
FROM People AS p
LEFT JOIN Places AS pl
WHERE ST_Distance(p.location, pl.location) < :distance
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top