Symfony/Doctrine:addJoinedEntityResult如何为一对多关系工作?

Nic*_*cki 5 doctrine native symfony

我有以下问题.我想这是误会.但谷歌搜索几个小时后没有找到解决方案我在这里发布.

我在Doctrine中有一个原生查询:

$rsm = new ResultSetMapping;
        $rsm->addEntityResult('Acme\CommentBundle\Entity\Comment', 'c');
        $rsm->addFieldResult('c', 'comment_id', 'id');
        $rsm->addFieldResult('c', 'slug', 'slug');
        $rsm->addFieldResult('c', 'comment', 'comment');
        $rsm->addFieldResult('c', 'created', 'created');
        $rsm->addJoinedEntityResult('Acme\AccountBundle\Entity\Worker', 'w', 'c', 'komments');
        $rsm->addFieldResult('w', 'worker_id', 'id');
        $rsm->addFieldResult('w', 'worker_name', 'name');
        $rsm->addJoinedEntityResult('Acme\CommentBundle\Entity\Document', 'd', 'c', 'documents');
        $rsm->addFieldResult('d', 'document_id', 'id');
        $rsm->addFieldResult('d', 'document_name', 'name');

        return $this->getEntityManager()
            ->createNativeQuery('SELECT t.id, c.id AS comment_id, c.slug, c.created, c.comment, c.worker_id AS comment_worker_id, c.created AS comment_created, d.id AS document_id, d.name AS document_name, w.id AS worker_id, w.name AS worker_name
            FROM comment_thread t
            INNER JOIN project p ON p.comment_thread_id = t.id
            LEFT JOIN comment c ON t.id = c.thread_id
            INNER JOIN worker w ON c.worker_id = w.id
            LEFT JOIN comment_document d ON c.id = d.comment_id
            WHERE p.id = :project_id
            ORDER BY c.created ASC', $rsm)
            ->setParameter('project_id', $
Run Code Online (Sandbox Code Playgroud)

不幸的是,第一个addJoinedEntityResult(Worker)无效.如果我删除它剩下的addJoinedEntityResult(文档)是完美的.

我想这是因为Document链接到Comment.所以评论是文件的"父母".在工人的情况下,反之亦然:评论是工人的"孩子",而不是文件的"父母".

换句话说: - 工人可以有多个注释 - 注释可以有多个文档

我的结果集应该以Comment为基础,与(一)工作者和(0 ... n)文档相关联.

但是如何在我的Doctrine Result Mapping中设置这种关系?

任何帮助表示赞赏:-)

尼基

And*_*sen -1

我看不到您的实体,所以我不确定这是否有任何帮助,但看起来您有一个拼写错误:

$rsm->addJoinedEntityResult('Acme\AccountBundle\Entity\Worker', 'w', 'c', 'komments');
Run Code Online (Sandbox Code Playgroud)

也许应该是

$rsm->addJoinedEntityResult('Acme\AccountBundle\Entity\Worker', 'w', 'c', 'comments');
Run Code Online (Sandbox Code Playgroud)

评论评论