The fact is that my users have ratings that are averaged through the MtM ratio, for example. a User has many Ratings , and the average value of the Rating object is the user rating.
What I want to do is figure out a way to somehow put this data into a user model, in a virtual field, or something else. I managed to create a non-mapped property and access methods for it so that the interface is solid and my looks look good. The property is not explicitly displayed in the Doctrine.
And then it seems that I have broken through every inner Doctrine. I looked at hydrators and ResultSetMappings, but there seems to be no good way to push the computed field into the object field.
So, here is what I managed to come up with.
Here I join and select an additional field:
$qb->select('u') ->addSelect('AVG(r.rating) AS people_rating') ->from('MyMainBundle:User', 'us') ->leftJoin('u.reviews', 'r', Expr\Join::WITH, 'r.user = u') ->where('u.id = :id') ->orderBy('people_rating', 'DESC') ;
What I need to get is Doctrine pushing people_rating into a User object. Now I get an ugly array that looks like this:
array(1) { [0] => array(2) { [0] => class My\MainBundle\Entity\User#868 (46) { protected $id => int(247) protected $email => string(28) " june68@goldneroconner.com.lc " private $createdAt => class DateTime#973 (3) (...) private $firstName => string(8) "John" (more elements)... } 'people_rating' => NULL } }
And here is what I really need:
array(1) { [0] => class My\MainBundle\Entity\User#868 (46) { protected $id => int(247) protected $email => string(28) " june68@goldneroconner.com.lc " private $createdAt => class DateTime#973 (3) (...) private $firstName => string(8) "John" private $peopleRating => (...) (my float calculated within MySQL AVG()) (more elements)... } }
I'm sure there is a way to implement ResultSetMapping that will do this, but how?