1
這裏是我的QueryBuilder查詢生成器生成不正確的查詢
$qb->select('u')
->from(Victim::class,'u')
->join('u.tags','t')
->where('t.id in (11,15)')
->groupBy('u.id')
->having('count(u.id)=2');
它會產生不正確的SQL查詢(看FROM子句)
SELECT
v0_.id AS id_0,
v0_.avatarUrl AS avatarurl_1,
v0_.displayName AS displayname_2,
v0_.summary AS summary_3,
v0_.gender AS gender_4,
v0_.birthDay AS birthday_5,
v0_.maritalState AS maritalstate_6,
v0_.folder_id AS folder_id_7
FROM victims v1_, victims v0_
INNER JOIN victim_tag v3_ ON v0_.id = v3_.victim_id
INNER JOIN tags t2_ ON t2_.id = v3_.tag_id
WHERE t2_.id IN (11,15)
GROUP BY v0_.id
HAVING count(v0_.id) = 2
但是當我使用DQL
$qry = sprintf(
'SELECT u FROM %s u JOIN u.%s t WHERE t.id IN (%s) GROUP BY u.id HAVING count(u.id)= %s ORDER BY u.id DESC',
Victim::class,
$key,
implode(',', $value),
count($value)
);
所得詢問是OK
SELECT
v0_.id AS id_0,
v0_.avatarUrl AS avatarurl_1,
v0_.displayName AS displayname_2,
v0_.summary AS summary_3,
v0_.gender AS gender_4,
v0_.birthDay AS birthday_5,
v0_.maritalState AS maritalstate_6,
v0_.folder_id AS folder_id_7
FROM victims v0_ INNER JOIN victim_tag v2_ ON v0_.id = v2_.victim_id
INNER JOIN tags t1_ ON t1_.id = v2_.tag_id
WHERE t1_.id IN (11, 15)
GROUP BY v0_.id
HAVING count(v0_.id) = 2
ORDER BY v0_.id DESC;
我有點困惑。我想在這裏使用QueryBuilder是因爲它的靈活性,如何使它正常工作?
您是否正在使用RepositoryClass中的查詢生成器?你重寫getAlias()方法嗎?你可以在課堂上使用洞方法嗎? –
@ViniciusZaramella否,它是外部存儲庫類,並且Repository類中的任何內容都未被覆蓋。 – sl4mmer
你是如何實現$ qb的?我認爲你可以擦除 - >從('...)中選擇('u') - >並在加入之後保留這些東西。 –