2010-11-17 49 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