2013-08-22 56 views
0

我有一個用戶實體和一個通過外鍵鏈接到用戶的UserData實體。Doctrine2使用WITH子句連接表

該用戶實體具有以下映射:

/** 
* @ORM\OneToOne(targetEntity="UserData", cascade={"persist", "remove"}) 
* @var integer 
*/ 
protected $userData; 

和userData具有簡單的ID字段:

/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
* @var integer 
*/ 
protected $id; 

確定;我想要檢索一個用戶實體,其UserData具有字段'NIN'值'A00000000';我試圖用這個DQL查詢:

SELECT u FROM eCommerceUserBundle:User u JOIN eCommerceUserBundle:UserData ud ON u.userData = ud.id WHERE ud.NIN = :NIN 

但我得到這個錯誤:

Expected Doctrine\ORM\Query\Lexer::T_WITH, got 'ON'

所以...我想戒菸ON子句,像這樣:

SELECT u FROM eCommerceUserBundle:User u JOIN eCommerceUserBundle:UserData ud WHERE ud.NIN = :NIN 

現在的問題是原始查詢主義正在:

SELECT 
    u0_.id AS id0, 
    u0_.email AS email1, 
    u0_.password AS password2, 
    u0_.created AS created3, 
    u0_.lastLogin AS lastLogin4, 
    u0_.confirmed AS confirmed5, 
    u0_.blocked AS blocked6, 
    u0_.particularData_id AS particularData_id7, 
    u0_.institutionalData_id AS institutionalData_id8, 
    u0_.administratorData_id AS administratorData_id9 
FROM 
    user u0_ 
    INNER JOIN userData u1_ ON (u1_.NIN = ?) 

所以看來,學說是推動我的WHERE子句進入ON子句...

我該怎麼辦?

回答

3

DQL查詢應該是這樣的:

SELECT u, ud 
FROM eCommerceUserBundle:User u 
JOIN u.userData ud 
WHERE ud.NIN = :NIN 

您可以連同他的用戶數據或沒有它從SELECT子句中除去ud選擇用戶 - User對象的數組將仍然返回,但隨着用戶數據填充從一開始 - 不選擇它將首先啓動對數據庫的新查詢$user->getUserData()