2012-03-27 95 views
1

我用createQuery()而不是queryBuilder創建查詢,但是我需要在查詢生成器中轉換查詢,以便在EntityType中生成表單。這裏是我的查詢:需要一些幫助將查詢從createQuery()轉換爲queryBuilder()

SELECT p FROM D2ECoreBundle:Player p, 
D2ECoreBundle:LadderMatch lm, 
D2ECoreBundle:PlayerComposition hpc 
JOIN hpc.players hp, 
D2ECoreBundle:PlayerComposition fpc 
JOIN fpc.players fp 
WHERE (lm.homePlayerComposition = hpc AND hp = p) 
OR (lm.foreignPlayerComposition = fpc AND fp = p) 

和這裏是我認爲這將是在QueryBuilder的,但不工作:

$qb->select('p') 
->from('D2ECoreBundle:Player', 'p') 
->from('D2ECoreBundle:LadderMatch', 'lm') 
->from('D2ECoreBundle:PlayerComposition', 'hpc') 
->join('hpc.players', 'hp') 
->from('D2ECoreBundle:PlayerComposition', 'fpc') 
->join('fpc.players', 'fp') 
->where('lm.homePlayerComposition = hpc' AND 'hp = p') 
->orwhere('lm.foreignPlayerComposition = fpc' AND 'fp = p'); 

有誰知道我應該改變已經工作?感謝您的答案!

回答

0

我最終設法做到了這一點。這裏是我的代碼:

$qb = $this->createQueryBuilder('lm') 
    ->select('p') 
    ->from('D2ECoreBundle:Player', 'p') 
    ->join('lm.homePlayerComposition', 'hpc') 
    ->join('hpc.players', 'hp') 
    ->join('lm.foreignPlayerComposition', 'fpc') 
    ->join('fpc.players', 'fp') 
    ->where('lm.homePlayerComposition = hpc AND hp = p') 
    ->orwhere('lm.foreignPlayerComposition = fpc AND fp = p'); 
return $qb; 

而且非常重要的事情,因爲我要選擇的球員,但由LadderMatch實體開始,我需要把這個LadderMatchRepository.php而不是PlayerRepository.php像我一樣,因爲它會影響基因組

1

當你查詢數據庫有:

SELECT * FROM TableA T1, TableB T2 .... 

在許多實現是一樣的:

SELECT * FROM TableA T1 JOIN TableB T2 ON ... // this is INNER JOIN 

所以,而是採用from多次,做joins

$qb->select('p') 
->from('D2ECoreBundle:Player', 'p') 
->join('p.ladderMatches', 'lm') 
->join('p.playerComposition', 'hpc') 
->join('hpc.players', 'hp') 
->join('hp.playerCompossition', 'fpc') 
->join('fpc.players', 'fp') 
->where('lm.homePlayerComposition = hpc' AND 'hp = p') 
->orwhere('lm.foreignPlayerComposition = fpc' AND 'fp = p'); 

不知道如果我得到的關係正確,但你明白了...

+0

玩家不包含LadderMatch,這就是問題所在。但是,我得到我的一個mystakes。梯隊比賽2球員組合(Home et Foreign)和球員組合包含球員。但我只是想要玩家! – copndz 2012-03-27 16:17:00