原则不允许ResultSetMappingBuilder工作 [英] Doctrine not allowing ResultSetMappingBuilder to work

查看:110
本文介绍了原则不允许ResultSetMappingBuilder工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony 2与教义。我已经设置了一个具有自定义查找功能的自定义存储库。当我加入一个子查询时,我非常确定我读过的内容,我需要使用ResultSetMappingBuilder从查询中获取结果。但是我收到以下错误:

 列id与映射器中的另一列冲突。 

我的代码是:

  $ 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查询运行在它自己没有任何问题,并返回我期望的结果。



更新,我已经通过重新映射addRootEntityFromClassMetadata上的id字段来修复ID问题,但是我仍然有一个修复的joinEntity的剩余问题:

  $ rsm-> addJoinedEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Ducks','ducks','c' COUNTRY_ID'); 

我收到以下错误:

 注意:未定义的索引:E中的country_id:\Documents\wfuk\Work\duck旅行\wamp\vendor\doctrine\lib\Doctrine\ORM\\ \\Internal\Hydration\ObjectHydrator.php行85 

我很确定这是到呼叫中的最后一个值,country_id,但我不是100%肯定这是什么。这里的说明: http://readthedocs.org/docs /doctrine-orm/en/latest/reference/native-sql.html 不太清楚,我完全不了解类docblock的描述。



它给出的错误是:

 列id与映射程序中的另一列冲突。 

然而,我已经为其中一个结果重命名了id字段,因此结果集只有一列名为'id'。



错误中的stacktrace显示它正在抛出:

 在ResultSetMappingBuilder  - > addAllClassFields('Wfuk\DuckBundle\Entity\Ducks','q',array())
在E:\Documents\wfuk\Work\鸭旅行\wamp\vendor\doctrine\lib\Doctrine\ORM\Query\ResultSetMappingBuilder.php在第71行


我怀疑这是很明显的,但是在这个功能的文档中没有太多可以在任何地方找到...



解决:



我不能将此添加为另外5个小时的答案,所以为了避免任何人浪费时间回答这个答案是:



对于任何有同样问题的人,第四个参数发送的问题是是行:

  $ rsm-> addJoinedEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Ducks','ducks' ,'c','ducks'); 

哪个应该在Country类中保留我正在使用的字段来存储duckC的arrayCollection

解决方案

刚刚解决问题:



对于任何有同样问题的人,问题是在这一行发送的第四个参数:

  $ rsm-> addJoinedEntityFromClassMetadata 'Wfuk \DuckBundle\Entity\Ducks','ducks','c','ducks'); 

哪个应该在Country类中保留我正在使用的字段来存储duckC的arrayCollection它。


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. 

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 runs on it's own without any problems and returns the results I'd expect.

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('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'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. 

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 ('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

I suspect it's something obvious but there's not much in the way of documentation on this function that I could find anywhere...

SOLVED:

I can't add this as an answer for another 5 hours, so to avoid anyone wasting time answering this the answer is:

For anyone who has the same problem the problem was with the 4th parameter sent on this line:

$rsm->addJoinedEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Ducks', 'ducks', 'c', 'ducks');

Which should have held the field within the Country class that I was using to store the arrayCollection of ducks within it.

解决方案

Just closing the question as it's solved:

For anyone who has the same problem the problem was with the 4th parameter sent on this line:

$rsm->addJoinedEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Ducks', 'ducks', 'c', 'ducks');

Which should have held the field within the Country class that I was using to store the arrayCollection of ducks within it.

这篇关于原则不允许ResultSetMappingBuilder工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆