2013-02-21 40 views
1

doctrine2我發現一些職位,在這裏,但它仍然不能正常工作:LEFT JOIN與在右側的表,在一對多

我有2個實體故事StoryCompleted:

TA\Minigames\MinigameBundle\Entity\Story: 
    type: entity 
    table: story 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    oneToMany: 
     completed: 
      targetEntity: StoryCompleted 
      mappedBy: story 
    fields: 
     cycle_nr: 
      type: integer 

TA\Minigames\MinigameBundle\Entity\StoryCompleted: 
    type: entity 
    table: story_completed 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    manyToOne: 
     story: 
      targetEntity: Story 
    fields: 
     company_id: 
      type: integer 
     completed: 
      type: boolean 

我想得到所有故事的列表,並知道哪些故事是由COMPANY_ID = 1可以看出,所以我嘗試了左連接:

SELECT s FROM MGMinigameBundle:Story s LEFT JOIN s.completed c WHERE s.cycle_nr = 1 AND (c.company_id = $company_id OR c is NULL) 

我沃爾德希望得到一個$ COMPANY_ID是這樣的:

story_id completed 
1   1 
2   - 
3   - 

但是,在每個Story中,我得到了所有完成的實體(對於所有$ company_id),而不是一個,對於給定的$ company_id,必須遍歷它們以找到我需要的實體。 :(

回答

1

包括在你的SELECT語句中的「C」:

SELECT s, c 
FROM MGMinigameBundle:Story s 
LEFT JOIN s.completed c 
WHERE s.cycle_nr = 1 AND (c.company_id = $company_id OR c is NULL) 

這樣的StoryCompleted記錄將被用於基礎上,COMPANY_ID「水合物」的故事對象的已完成收集這之間的區別"regular join" and a "fetch join" in DQL