我正在使用 Symfony 2 和教义.我已经设置了一个带有自定义查找功能的自定义存储库.当它加入子查询时,我很确定从我读过的内容来看,我需要使用 ResultSetMappingBuilder 从查询中获取结果.但我不断收到以下错误:
I'm using Symfony 2 with doctrine. I've set up a custom repository with a custom find function. As it joins to a subquery I'm pretty sure from what I've read that I'll need to use the 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.
我的代码是:
$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 DoctrineORMQueryResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata('WfukDuckBundleEntityCountry', 'c', array('id' => 'cid'));
$rsm->addJoinedEntityFromClassMetadata('WfukDuckBundleEntityDucks', 'ducks', 'c', 'country_id');
$query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
return $query->getResult();
SQL 查询自行运行,没有任何问题,并返回我期望的结果.
The SQL query runs on it's own without any problems and returns the results I'd expect.
更新,我已经通过重新映射 addRootEntityFromClassMetadata 上的 id 字段修复了 id 问题,但我仍然遇到了修改后的 joinEntity 的遗留问题:
UPDATE, I've fixed the id issue by remapping the id field on the addRootEntityFromClassMetadata, but I've still got a remaining problem with the revised joinedEntity:
$rsm->addJoinedEntityFromClassMetadata('WfukDuckBundleEntityDucks', 'ducks', 'c', 'country_id');
我收到以下错误:
Notice: Undefined index: country_id in E:DocumentswfukWorkduck travelswampvendordoctrinelibDoctrineORMInternalHydrationObjectHydrator.php line 85
我很确定这是调用中的最后一个值 country_id,但我不能 100% 确定这是什么.这里的解释:http://readthedocs.org/docs/doctrine-orm/en/latest/reference/native-sql.html 不太清楚,我不完全理解类 docblock 的描述.
I'm pretty sure this is down to the last value in the call, country_id, but I'm not 100% sure what this is. The explanation here: http://readthedocs.org/docs/doctrine-orm/en/latest/reference/native-sql.html isn't too clear and I don't fully understand the description from the class docblock.
它给出的错误是:
The error it gives is:
The column 'id' conflicts with another column in the mapper.
然而,我已经为其中一个结果重命名了 id 字段,因此结果集只有一列名为id".
Yet I have renamed the id field for one of the results so the result set only has one column called 'id'.
错误的堆栈跟踪显示它被抛出这里:
The stacktrace on the error shows it's being thrown here:
at ResultSetMappingBuilder ->addAllClassFields ('WfukDuckBundleEntityDucks', 'q', array())
in E:DocumentswfukWorkduck travelswampvendordoctrinelibDoctrineORMQueryResultSetMappingBuilder.php at line 71
我怀疑这很明显,但是我可以在任何地方找到有关此功能的文档的方式并不多......
I suspect it's something obvious but there's not much in the way of documentation on this function that I could find anywhere...
已解决:
我无法在 5 小时内将其添加为答案,因此为避免任何人浪费时间回答此问题,答案是:
I can't add this as an answer for another 5 hours, so to avoid anyone wasting time answering this the answer is:
对于任何有同样问题的人来说,问题出在此行上发送的第 4 个参数上:
For anyone who has the same problem the problem was with the 4th parameter sent on this line:
$rsm->addJoinedEntityFromClassMetadata('WfukDuckBundleEntityDucks', 'ducks', 'c', 'ducks');
我用来在其中存储鸭子的 arrayCollection 的 Country 类中应该包含该字段.
Which should have held the field within the Country class that I was using to store the arrayCollection of ducks within it.
刚刚解决了这个问题:
对于任何有同样问题的人来说,问题出在此行上发送的第 4 个参数上:
For anyone who has the same problem the problem was with the 4th parameter sent on this line:
$rsm->addJoinedEntityFromClassMetadata('WfukDuckBundleEntityDucks', 'ducks', 'c', 'ducks');
我用来在其中存储鸭子的 arrayCollection 的 Country 类中应该包含该字段.
Which should have held the field within the Country class that I was using to store the arrayCollection of ducks within it.
这篇关于学说不允许 ResultSetMappingBuilder 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!