Answer the question
In order to leave comments, you need to log in
Calculate average relation field?
https://www.doctrine-project.org/projects/doctrine...
$query->addSelect(['m', $query->expr()->avg('r.distance') . ' as distance'])
->from(Member::class, 'm')
;
$query->join('m.runs', 'r');
$query->addOrderBy($query->expr()->avg('r.distance'), 'ASC');
$result =$query->getQuery()->getResult();
foreach ($result as $item) {
dump($item['distance']);
}
die;
Answer the question
In order to leave comments, you need to log in
Be sure to add
$query->addGroupBy('m.id');
before sorting, without this it gave an incomprehensible result, 1 record of the 2nd user and 3 km, instead of 2 users and averages
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question