我有以下问题。我想这是误解。但是在谷歌搜索了几个小时没有找到解决方案后,我把它贴在这里。
我在 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', $
不幸的是,第一个 addJoinedEntityResult (Worker) 不起作用。如果我删除它,剩下的 addJoinedEntityResult (Document) 就完美了。
我猜这是因为文档链接到评论。所以评论是文档的“父”。对于 Worker,反之亦然: Comment 是 Worker 的“子”,而不是 Document 的“父”。
换句话说: - 一个工人可以有多个评论 - 一个评论可以有多个文档
我的结果集应该以 Comment 为基础,与 (one) worker 和 (0 .. n) 文档相关联。
但是我如何在我的 Doctrine Result Mapping 中建立这种关系呢?
任何帮助表示赞赏:-)
尼基