2010-11-17 50 views
0

我有两个学说实体'用户'和'属性',如下所示。我需要构造一个查询来检索所有用户,并按属性名称排序,其中属性type = x。例如,获取所有用户并按'标题'排序。教条2按关联标签排序

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; 

} 

回答

0

我觉得这个查询你在找什么:

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

这将不会返回所有用户。它只会返回具有'title'类型属性的用户。假设你有三个用户:john,ringo,paul。约翰有一个属性,类型为“标题”和名字“作家”,ringo:键入“标题”和名字“鼓手”,保罗没有标题。现在,结果依次显示,ringo(鼓手),john(作家),paul(null) – waigani 2010-11-18 04:29:26

+0

嗨waigani,你尝试了一个左加入吗? SELECT u FROM User u LEFT JOIN u.attributes a WITH a.type ='title'ORDER BY a.name ASC – saadtazi 2010-12-15 14:58:44