相关疑难解决方法(0)

Doctrine不允许ResultSetMappingBuilder工作

我正在使用Symfony 2和学说.我已经设置了一个带有自定义查找功能的自定义存储库.当它加入子查询时,我非常肯定从我读过的内容中我需要使用ResultSetMappingBuilder从查询中获取结果.但我不断收到以下错误:

The column 'id' conflicts with another column in the mapper. 
Run Code Online (Sandbox Code Playgroud)

我的代码是:

    $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();
Run Code Online (Sandbox Code Playgroud)

SQL查询在它自己运行时没有任何问题,并返回我期望的结果.

更新,我已经通过重新映射addRootEntityFromClassMetadata上的id字段修复了id问题,但我仍然遇到修改后的joinedEntity的问题:

    $rsm->addJoinedEntityFromClassMetadata('Wfuk\DuckBundle\Entity\Ducks', 'ducks', 'c', 'country_id');
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Notice: Undefined index: country_id in …
Run Code Online (Sandbox Code Playgroud)

php doctrine symfony doctrine-orm

6
推荐指数
2
解决办法
7162
查看次数

标签 统计

doctrine ×1

doctrine-orm ×1

php ×1

symfony ×1