Doctrine 2 Query Builder与硬编码SQL - 不同的结果

时间:2011-03-10 15:30:22

标签: php doctrine-orm dql

我有Doctrine2 DQL查询但是我想用QueryBuilder构建它,我注意到生成的DQL与手工制作的DQL有些不同,我想知道我在这里缺少什么 - 也许我不知道什么还是做错了?

好的,一些细节:

我手工制作的查询如下所示:

select count(fi.id) 
from Entities\Content\FolderLookup fl 
join fl.site fls 
join fl.folder flf,
Entities\Content\FolderItem fi 
join fi.site fis 
join fi.folder fif 
join fi.item it 
join it.type tp 
join it.content ic 
where fl.namePath = ?1 
and tp.name = ?2 
and fls.id = fis.id 
and flf.id = fif.id

现在,我正在尝试使用QueryBuilder重现它:

$qb->select("count(fi.id)")->from("Entities\Content\FolderLookup", "fl")->join("fl.site","fls")->join("fl.folder", "flf");

$qb->from("Entities\Content\FolderItem","fi")->join("fi.site","fis")->join("fi.folder","fif");
$qb->join("fi.item","it")->join("it.type","tp")->join("it.content","ic");

$wherePart = $qb->expr()->andx();
$wherePart->add($qb->expr()->eq("fl.namePath","?1"));
$wherePart->add($qb->expr()->eq("tp.name","?2"));
$wherePart->add($qb->expr()->eq("fls.id","fis.id"));
$wherePart->add($qb->expr()->eq("flf.id","fif.id"));

$qb->where($wherePart);

然而,这产生了这个DQL查询:

SELECT count(fi.id) FROM Entities\Content\FolderLookup fl, 
Entities\Content\FolderItem fi 
INNER JOIN fl.site fls 
INNER JOIN fl.folder flf 
INNER JOIN fi.site fis 
INNER JOIN fi.folder fif 
INNER JOIN fi.item it 
INNER JOIN it.type tp 
INNER JOIN it.content ic 
WHERE (fl.namePath = ?1) 
AND (tp.name = ?2) 
AND (fls.id = fis.id) 
AND (flf.id = fif.id)

正如你所看到的,有一部分缺失的手工制作(第一行):

fl join fl.site fls join fl.folder flf

我不确定为什么这些连接会丢失,因为我在这里定义它们:

$qb->select("count(fi.id)")->from("Entities\Content\FolderLookup", "fl")->join("fl.site","fls")->join("fl.folder", "flf");

更新

有趣的部分开始,当DQL被翻译成SQL时 - 在这种情况下MySQL:

手工制作:

SELECT count(f0_.id) AS sclr0 FROM FolderLookup f1_ INNER JOIN Site s2_ ON f1_.site_id = s2_.id INNER JOIN Folder f3_ ON f1_.folder_id = f3_.id, FolderItem f0_ INNER JOIN Site s4_ ON f0_.site_id = s4_.id INNER JOIN Folder f5_ ON f0_.folder_id = f5_.id INNER JOIN Item i6_ ON f0_.item_id = i6_.id INNER JOIN ItemType i7_ ON i6_.type_id = i7_.id INNER JOIN ItemContent i8_ ON i6_.content_id = i8_.id WHERE f1_.namePath = ? AND i7_.name = ? AND s2_.id = s4_.id AND f3_.id = f5_.id

生成的内容如下所示:

SELECT count(f0_.id) AS sclr0 FROM FolderLookup f1_, FolderItem f0_ INNER JOIN Site s2_ ON f1_.site_id = s2_.id INNER JOIN Folder f3_ ON f1_.folder_id = f3_.id INNER JOIN Site s4_ ON f0_.site_id = s4_.id INNER JOIN Folder f5_ ON f0_.folder_id = f5_.id INNER JOIN Item i6_ ON f0_.item_id = i6_.id INNER JOIN ItemType i7_ ON i6_.type_id = i7_.id INNER JOIN ItemContent i8_ ON i6_.content_id = i8_.id WHERE (f1_.namePath = ?) AND (i7_.name = ?) AND (s2_.id = s4_.id) AND (f3_.id = f5_.id)

这是无效语句,因为数据库返回:

Column not found: 1054 Unknown column 'f1_.site_id' in 'on clause'

欢迎任何想法。

2 个答案:

答案 0 :(得分:1)

似乎DQL解析器错误地将连接定位为错误。

我最初的建议是尝试只制作1个FROM项和一个subselect。 另外,如果您在我们的错误跟踪中添加您在此处提出的相同内容,我很高兴:http://www.doctrine-project.org/jira/browse/DDC

非常感谢!

Guilherme Blanco Doctirne核心开发人员

答案 1 :(得分:0)

他们并没有失踪。刚重新订购

  

INNER JOIN fl.site fls   INNER JOIN fl.folder flf