I am using Symfony 2 with a doctrine. I created a custom repository with a custom search function. When he joins the subquery, I'm pretty sure I read that I will need to use ResultSetMappingBuilder to get the results back from the query. But I keep getting the following error:
The column 'id' conflicts with another column in the mapper.
My code is:
$sql ="SELECT c.id as cid, c.country, c.rank, c.continent, q.* FROM country c INNER JOIN ( SELECT d.* , MAX( d.rating ) AS maxrating FROM ducks d GROUP BY d.country_id )q ON ( q.country_id = c.id )"; $rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->getEntityManager()); $rsm->addRootEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Country', 'c', array('id' => 'cid')); $rsm->addJoinedEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Ducks', 'ducks', 'c', 'country_id'); $query = $this->getEntityManager()->createNativeQuery($sql, $rsm); return $query->getResult();
The SQL query is executed without any problems and returns the results that I expect.
UPDATE, I fixed the authentication problem by reassigning the id field to addRootEntityFromClassMetadata, but I still had a problem with the reconsidered connection:
$rsm->addJoinedEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Ducks', 'ducks', 'c', 'country_id');
I get the following error:
Notice: Undefined index: country_id in E:\Documents\wfuk\Work\duck travels\wamp\vendor\doctrine\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php line 85
I am pretty sure this is the last value in the country_id call, but I'm not 100% sure what it is. The explanation is here: http://readthedocs.org/docs/doctrine-orm/en/latest/reference/native-sql.html is not too clear, and I do not fully understand the description from the docblock class.
The error it gives is:
The column 'id' conflicts with another column in the mapper.
But I renamed the id field for one of the results, so in the result set there is only one column named 'id'.
The stack on error shows that it is selected here:
at ResultSetMappingBuilder ->addAllClassFields ('Wfuk\DuckBundle\Entity\Ducks', 'q', array()) in E:\Documents\wfuk\Work\duck travels\wamp\vendor\doctrine\lib\Doctrine\ORM\Query\ResultSetMappingBuilder.php at line 71
Strike> I suspect this is something obvious, but there are not many documents on this function that I could find anywhere ...
DECIDE:
I cannot add this as an answer for another 5 hours, therefore, to avoid wasting time on the answer, the answer is:
For those who have the same problem, the problem is related to the 4th parameter sent on this line:
$rsm->addJoinedEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Ducks', 'ducks', 'c', 'ducks');
Which should have contained a field inside the Country class that I used to store the array. Duck inside him.