1

我在这里找到了一些帖子,但它仍然不起作用:

我有 2 个实体故事和故事已完成:

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         -

但是对于给定的 $company_id,我在每个 Story 中都获得了所有已完成的实体(对于所有 $company_id),而不仅仅是一个,并且必须遍历它们以找到我需要的实体。:(

4

1 回答 1

1

在您的选择语句中包含“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 “水合” Story 对象的已完成集合。这是DQL 中“常规连接”和“获取连接”之间的区别

于 2013-02-21T20:36:32.550 回答