2012-05-03 49 views
0

我已經跟着這個網站的一些問題,仍然無法找出我的設置有什麼問題。我被困了一段時間。對不起,如果這是一個基本的錯誤,我還是新的教義。謝謝。doctrine 2加入協會找不到

我得到的錯誤是:

Error: Class models\\Classes_users has no association named users_username... 

查詢是:

SELECT u FROM models\Classes_users c JOIN c.users_username u WHERE c.class_id = 1 

的實體:

/** 
* models\Classes_users 
* 
* @Table(name="classes_users") 
* @Entity 
*/ 
class Classes_users 
{ 
    ... 

    /** 
    * @var string $users_username 
    * 
    * @Id 
    * @ManyToOne(targetEntity="Users", inversedBy="users_username") 
    * @JoinColumn(name="users_username", referencedColumnName="users_username") 
    * @Column(name="users_username", type="integer", precision=0, scale=0, nullable=false, unique=false) 
    */ 
    private $users_username; 

    ... 
} 



/** 
* models\Users 
* 
* @Table(name="users") 
* @Entity 
*/ 
class Users 
{ 
    .... 

    /** 
    * @var string $users_username 
    * @Id 
    * @OneToMany(targetEntity="Classes_users", mappedBy="users_username") 
    * @Column(name="users_username", type="string") 
    */ 
    private $users_username; 

    .... 
} 
+0

您是否願意告訴我們您希望在此獲得什麼?可能是這裏的人可以幫助您解決所需的映射和註釋。 – Broncha

回答

2

我不知道到底要完成什麼,但我認爲你的實體設計有點「奇怪」,我在這裏發佈一些代碼供你參考:

/** 
* models\Classes_users 
* 
* @Table(name="classes_users") 
* @Entity 
*/ 
class Classes_users 
{ 
    ... 
    /** 
    * @Id 
    */ 
    private $class_id; // use other varible as Id 

    /** 
    * @ManyToOne(targetEntity="Users", inversedBy="user_usernames") 
    * @JoinColumn(name="user_id", referencedColumnName="id") 
    **Delect this line =>** * @Column(name="users_username", type="integer", precision=0, scale=0, nullable=false, unique=false) 
    */ 
    private $user; // change name to user, this varible is a virtual variable, only make sense in php objects, there would not be a real column in database, but a user_id column produced by Doctrine instead 

    ... 
} 



/** 
* models\Users 
* 
* @Table(name="users") 
* @Entity 
*/ 
class Users 
{ 
    .... 
    /** 
    * @Id 
    * 
    */ 
    $private id; // use other variable as Id 


    /** 
    * @var string $users_username 
    * @Id 
    * @OneToMany(targetEntity="Classes_users", mappedBy="user") 
    **Delect this line =>** * @Column(name="users_username", type="string") 
    */ 
    private $user_usernames; // suggest change name to reflect the one to many associations, this variable should be an arraycollection, and there would not be a real column in database 

    public function __construct() 
    { 
     $this->user_usernames = new ArrayCollection(); 
    } 

    .... 
} 

現在,您可以查詢更改爲:

選擇[u] FROM \用戶üJOIN u.user_usernames C型WHERE c.class_id = 1

我不知道,如果你使用的Symfony,但我建議您閱讀this link