我想在Doctrine SQL中做类似的事情......
SELECT N.id as n_id, C.id as c_id, T.id as t_id, C.author_id, C.body as comment FROM `news` N
LEFT JOIN thread T on T.id = N.id
LEFT JOIN comment C on C.thread_id = T.id
WHERE 1
所以我做到了:
$rsm = new ResultSetMapping;
$rsm->addEntityResult('App\MyBundle\Entity\News', 'N');
$cols = array ('id', 'title','date');
foreach ($cols as $key => $col) {
$rsm->addFieldResult('N', $col, $col);
}
// thread
$rsm->addJoinedEntityResult('App\MyBundle\Entity\Thread' , 'T', 'N', 'thread');
$rsm->addFieldResult('T', 't_id', 'id');
$sql = 'SELECT N.id, N.title, N.date, T.id AS t_id FROM news N ' .
'LEFT JOIN thread T ON T.id = N.id WHERE N.id = 1';
$query = $this->_em->createNativeQuery($sql, $rsm);
但我有一个错误:
Notice: Undefined index: thread in /vendor/doctrine/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php line 85
我想确切地知道两个实体之间没有成员链接。
有什么想法吗?
全部谢谢
见你
山姆
答案 0 :(得分:2)
根据文件:
第四个和最后一个参数是父实体结果中应包含已连接实体结果的字段的名称。
参考:http://www.doctrine-project.org/docs/orm/2.1/en/reference/native-sql.html#joined-entity-results
您可能会错过在thread
类中定义将收到您加入的实体的News
属性。