doctrine 2 orderby associative table

时间:2010-11-17 02:31:48

标签: sql-order-by dql doctrine-orm

我有两个学说实体'User'和'Attribute',如下所示。我需要构建一个查询来检索所有用户,并按属性名称= x的属性名称对它们进行排序。例如,获取所有用户并按“标题”对其进行排序。

SELECT u FROM User u JOIN u.attributes a ORDER BY a.name {something??} a.type = 'title'


class User {

    /**
     * @ManyToMany (targetEntity="Attribute", inversedBy="users", cascade={"persist"})
     * 
     */
    private $attributes;
}



class Attribute {

    /**
     * @Column (type="string", length=255, unique=false, nullable=false, name="name")
     * @FormElement (type="text")
     * @type string
     */
    protected $name;

    /**
     * @Column (type="string", unique=false, nullable=true, name="type")
     * @type string
     */
    private $type;

    /**
     * @Column (type="integer", length=11, unique=false, nullable=true, name="priority")
     * @type integer
     */
    private $priority;

    /**
     * @ManyToMany (targetEntity="User", mappedBy="attributes")
     */
    private $users;

}

1 个答案:

答案 0 :(得分:0)

我认为这个查询就是你要找的:

SELECT u FROM User u JOIN u.attributes a WHERE a.type = 'title' ORDER BY a.name ASC