【问题标题】:Doctrine not allowing ResultSetMappingBuilder to work不允许 ResultSetMappingBuilder 工作的原则
【发布时间】:2012-03-06 09:12:48
【问题描述】:

我将 Symfony 2 与教义一起使用。我已经建立了一个具有自定义查找功能的自定义存储库。当它加入子查询时,我很确定从我所读到的内容中,我需要使用 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 查询自行运行,没有任何问题,并返回我期望的结果。

更新,我已经通过重新映射 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 不太清楚,我不完全理解类文档块的描述。

它给出的错误是:

The column 'id' conflicts with another column in the mapper. 

但我已为其中一个结果重命名了 id 字段,因此结果集只有一列称为“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');

这应该包含 Country 类中的字段,我用来在其中存储鸭子的 arrayCollection。

【问题讨论】:

    标签: php symfony doctrine doctrine-orm


    【解决方案1】:

    在创建ResultSetMappingBuilder 时指定COLUMN_RENAMING_INCREMENTrename 模式以使Doctrine 自动解决冲突

    那么在实际查询中你需要使用选择生成器:

        $rsm = new ResultSetMappingBuilder(
            $entityManager, 
            ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT
        );
    
        $rsm->addRootEntityFromClassMetadata("Product", "p");
        $rsm->addJoinedEntityFromClassMetadata("ProductImage", "pi", "p", "images");
    
        $query = $entityManager->createNativeQuery("
            SELECT " . $rsm->generateSelectClause() . " 
            FROM product p 
            JOIN product_image pi
        ", $rsm);
    

    【讨论】:

    • 我错过了$rsm->generateSelectClause() 并且浪费了太多时间来提及。感谢这篇文章。
    【解决方案2】:

    在问题解决后结束:

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

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

    这应该包含 Country 类中的字段,我用来在其中存储鸭子的 arrayCollection。

    【讨论】:

    • 我也有同样的问题,但不太明白你的回答。
    • 请改进答案,以便对其他人有用。
    猜你喜欢
    • 2012-04-16
    • 2018-11-06
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多