1

How to do this query in Doctrine 2 QueryBuilder:

SELECT AVG(x.distance) avg_distance FROM (SELECT r.* FROM result r WHERE r.place_id = ? GROUP BY r.place_id ORDER BY r.id DESC LIMIT 100

I try this:

$dql = $qb ->select('r.*') ->from('CoreBundle:Result', 'r') ->where('r.place = :place') ->orderBy('r.id', 'DESC') ->setMaxResults(100) ->setParameter('place', $place) ->getDQL() ; $result = $qb ->select('AVG(x.distance) avg_distance') ->from($dql, 'x') ->getQuery() ->getArrayResult(); 

but not work

SELECT r.* FROM': Error: Class 'SELECT' is not defined. 
0

1 Answer 1

1
$sql = "SELECT AVG(x.distance) avg_distance FROM (SELECT r.* FROM result r WHERE r.place_id = :place_id ORDER BY r.id DESC LIMIT 100) x "; $stmt = $this->em->getConnection()->prepare($sql); $stmt->bindValue(':place_id', $place->getId()); $stmt->execute(); return $stmt->fetch(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.