加入一张桌子

时间:2016-05-13 13:25:38

标签: doctrine symfony

我正在尝试编写一个学说查询来查找parent_id是给定Skill的所有Skill

这是可以实现我想要的SQL:

SELECT *
FROM skill s1  
JOIN skill s2 on s2.parent_id = s1.id
WHERE s1.name = "somename"

以下是我到目前为止使用的doctrine查询构建器:

$childCategories = $em->getRepository("AppBundle:Skill")
    ->createQueryBuilder("s1")
    ->join("AppBundle:Skill", "s2", "WITH", "s2.parent = s1.id")
    ->where("s1.name = :parentName")
    ->setParameter("parentName", "somename")
    ->getQuery()
    ->getResult();

但是我没有得到任何结果,正确的学说查询是什么?

编辑:这是我的学说orm表结构:

AppBundle\Entity\Skill:
type: entity
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 100
            unique: true
    manyToOne:
        parent:
            nullable: true
            targetEntity: Skill
            joinColumn:
                name: parent_id
                referencedColumnName: id

1 个答案:

答案 0 :(得分:1)

获取相关实体意味着您需要添加将实体相互比较的条件(您的查询将实体与ID进行比较):

$childCategories = $em->getRepository("AppBundle:Skill")
    ->createQueryBuilder("s1")
    ->join("AppBundle:Skill", "s2", "WITH", "s2.parent = s1")
    ->where("s1.name = :parentName")
    ->setParameter("parentName", "somename")
    ->getQuery()
    ->getResult();