我使用Symfony 2与原则。我使用自定义查找功能设置了自定义存储库。当它加入到一个子查询中时,我确信从我读过的内容中,我将需要使用ResultSetMappingBuilder从查询中返回结果。不过,我不断收到以下错误:不允许ResultSetMappingBuilder工作的原则
The column 'id' conflicts with another column in the mapper.
我的代码是:
$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();
的SQL查询它自己的运行没有任何问题,并返回我期望的结果。
UPDATE,我已经通过重新映射在addRootEntityFromClassMetadata id字段固定的ID问题,但我还有经修订的joinedEntity剩余问题:
$rsm->addJoinedEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Ducks', 'ducks', 'c', 'country_id');
我得到以下错误:
Notice: Undefined index: country_id in E:\Documents\wfuk\Work\duck travels\wamp\vendor\doctrine\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php line 85
我敢肯定,这是调用country_id的最后一个值,但我不是100%确定这是什么。这里的解释:http://readthedocs.org/docs/doctrine-orm/en/latest/reference/native-sql.html不太清楚,我不完全理解类docblock的描述。
它给人的错误是:
The column 'id' conflicts with another column in the mapper.
但我已经改名为id字段的结果之一,所以结果只设置了所谓的“身份证”一列。
上错误的堆栈跟踪显示它在这里被抛出:
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
我怀疑这是明显的东西,但没有太多关于这个功能的文档的方式,我能找到的任何地方...
解决:
我不能添加此为5小时的答案,所以要避免任何人浪费时间回答这个问题的答案是:
对于任何人谁具有同样的问题,这个问题是与这条线发送的第四个参数:
$rsm->addJoinedEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Ducks', 'ducks', 'c', 'ducks');
本应认为,我用的是存储内的鸭子ArrayCollection的国家类中的字段它。
我有同样的问题,但不太理解你的答案。 – 2014-05-06 15:43:29
请改善答案,以便对他人有用。 – marcosdsanchez 2016-02-03 22:43:14