My normal query was looking like this.
$qb = $placeRepository->createQueryBuilder('p');
var_dump($qb->getQuery()->getResult());
I'll get few results as objects. So this is the normal behaviour.
Then I want to add a custom field with ResultSetMapping
.
$qb = $placeRepository->createQueryBuilder('p');
$qb->addSelect('123 as distance');
$rsm = new ResultSetMapping;
$rsm->addEntityResult(Place::class, 'p');
$rsm->addFieldResult('p', 'id', 'id');
$rsm->addScalarResult('distance', 'distance');
var_dump($qb->getQuery()->setResultSetMapping($rsm)->getResult());
With a ResultSetMappingBuilder
its also not working.
$qb = $placeRepository->createQueryBuilder('p');
$qb->addSelect('123 as distance');
$rsm = $placeRepository->createResultSetMappingBuilder('p');
var_dump($qb->getQuery()->setResultSetMapping($rsm)->getResult());
Not working means: Array with zero items in it.