2013-05-14 57 views
0

我的多對多關係存在問題。我想要訪問查詢構建器查詢的參考表。有了多對多的關係,我無法訪問我的參考表,所以我建立了兩個一對多的關係。我的結構上看喜歡:與參考表(Doctrine 2,ZF2)的兩個一對多關係

User ---> UserUserCategory <--- UserCategory

上述結構有兩個一個一對多的關係,並與數據庫工作的罰款。當我有與數據庫(在UserUserCategory)以下數據的用戶:

Table User 
ID | Name 
1 | Bart 
2 | Gerard 

Table Category 
ID | Name 
1 | Officer 
2 | Medic 

Table UserUserCategory 
User | Category 
1 | 1 
2 | 2 

所以巴特是官和傑拉德是一名醫生。但是當我想要檢索數據時,它說Bart是Medic,Gerard在這個類別中有一個「空值」。

我的用戶實體:

/** 
* Entity Class representing a post of our User module. 
* 
* @ORM\Entity 
* @ORM\Table(name="user") 
* @ORM\Entity(repositoryClass="User\Repository\UserRepository") 
* 
*/ 
class User extends zfcUser implements UserInterface 
{ 
    /** 
    * Categories from user 
    * 
    * @ORM\OneToMany(targetEntity="User\Entity\UserUserCategory", mappedBy="user_id", cascade={"remove", "persist"}) 
    * @var UserUserCategory 
    * @access protected 
    */ 
    protected $user_usercategories; 

//name & user_id comes here 

    /** 
    * Constructor to make a new ArrayCollection for addresses 
    * 
    * 
    */ 
    public function __construct() 
    {   
     $this->user_usercategories = new ArrayCollection(); 
    } 

     /** 
    * @param Collection $categories 
    */  
    public function addUserUserCategories(Collection $user_usercategories) 
    { 
     foreach ($user_usercategories as $user_usercategorie) { 
      $user_usercategorie->setUser($this); 
      $this->user_usercategories->add($user_usercategorie); 
     } 
    } 

    /** 
    * @param Collection $categories 
    */ 
    public function removeUserUserCategories(Collection $user_usercategories) 
    { 
     foreach ($user_usercategories as $user_usercategorie) { 
      $user_usercategorie->setUser(null); 
      $this->user_usercategories->removeElement($user_usercategorie); 
     } 
    } 

    /** 
    * @return Collection 
    */ 
    public function getUserUserCategories() 
    { 
     return $this->categories; 
    } 
} 

我UserCategory實體:

/** 
* A User category entity. 
* @ORM\Entity 
* @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="unique_name_parentId", columns={"name", "parent_id"})}) 
* @ORM\HasLifecycleCallbacks 
*/ 
class UserCategory extends Category 
{  
    /** 
    * User_usercategories 
    * 
    * @ORM\OneToMany(targetEntity="User\Entity\UserUserCategory", mappedBy="category_id") 
    * @var UserUserCategory 
    * @access protected 
    */ 
    protected $user_usercategories; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->user_usercategories = new ArrayCollection(); 
    } 

    /** 
    * @param Collection $categories 
    */  
    public function addUserUserCategories(Collection $user_usercategories) 
    { 
     foreach ($user_usercategories as $user_usercategorie) { 
      $user_usercategorie->setCategory($this); 
      $this->user_usercategories->add($user_usercategorie); 
     } 
    } 

    /** 
    * @param Collection $categories 
    */ 
    public function removeUserUserCategories(Collection $user_usercategories) 
    { 
     foreach ($user_usercategories as $user_usercategorie) { 
      $user_usercategorie->setCategory(null); 
      $this->user_usercategories->removeElement($user_usercategorie); 
     } 
    } 

    /** 
    * @return Collection 
    */ 
    public function getUserUserCategories() 
    { 
     return $this->categories; 
    } 
} 

我UserUserCategory實體:

/** 
* Entity Class representing a post of our User_UserCategory entity. 
* 
* @ORM\Entity 
* @ORM\Table(name="user_usercategory") 
* 
*/ 
class UserUserCategory 
{ 
    /** 
    * User with a category 
    * 
    * @ORM\ManyToOne(targetEntity="User\Entity\User", inversedBy="user_usercategories") 
    * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", nullable=false, onDelete="CASCADE") 
    * @ORM\Id 
    * 
    * @var User 
    * @access protected 
    */ 
    protected $user_id; 

    /** 
    * Category from user 
    * 
    * @ORM\ManyToOne(targetEntity="User\Entity\UserCategory", inversedBy="user_usercategories") 
    * @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false, onDelete="CASCADE") 
    * @ORM\Id 
    * 
    * @var Category 
    * @access protected 
    */ 
    protected $category_id; 

     public function getUser() 
    { 
     return $this->user; 
    } 

    /** 
    * Set User 
    * 
    * @param User $user 
    * @return User 
    */ 
    public function setUser(User $user = null) 
    { 
     //die('setUser'); 
     $this->user = $user; 
     return $this; 
    } 

    public function getCategory() 
    { 
     return $this->category; 
    } 

    /** 
    * Set Category 
    * 
    * @param Category $category 
    * @return Category 
    */ 
    public function setCategory(Category $category = null) 
    { 
     $this->category = $category; 
     return $this; 
    } 
} 

當我執行以下行時,它會返回錯誤的結果。彈出錯誤的類別:

\ Doctrine \ Common \ Util \ Debug :: dump($ this-> getEntityManager() - > find('User \ Entity \ User','49') - > user_usercategories); 死亡;

結果:

array(1) { 
    [0]=> 
    object(stdClass)#452 (3) { 
    ["__CLASS__"]=> 
    string(28) "User\Entity\UserUserCategory" 
    ["user_id"]=> 
    string(16) "User\Entity\User" 
    ["category_id"]=> 
    string(24) "User\Entity\UserCategory" 
    } 
} 

在CATEGORY_ID是軍醫印刷,我希望官員要回。

在我的其他用戶(id = 60)中,category_id字段爲「null」。所以它看起來很喜歡Doctrine跳過我UserUserCategory中的第一個輸入,從第二個開始,不能再獲得最後一個類。

回答

1

沒有冒犯,但我覺得你的代碼很難閱讀。我建議你做一些修正,甚至可以幫助你解決問題。

1:命名:代替UserCategory,將其重命名爲Category。像

public function addCategory(Category $category) 
{ 
    $reference = new UserCategory() ; 
    $reference->setUser($this) ; 
    $reference->setCategory($category) ; 
    $this->user_categories->add($reference) ; 
} 

public function removeCategory(Category $category) 
{ 
    foreach($this->user_categories as $reference) { 
     if ($reference->getCategory() === $category) 
      $this->user_categories->removeElement($reference) ; 
    } 
} 

Symfony2的相反addCategories(收集$陣列)的,做單一版本automaticaly:如果你的等級都會有不同的類型,創建constansts與值的新欄目 「型」 之類

class Category 
{ 
    const TYPE_USER = 1 ; 
    .... 

2認識到這樣的方法。即使你的關係是複數(如類別),S2會發現singularified addCategory和removeCategory方法。

要獲得類別的陣列,使用此:

public function getCategories() 
{ 
    $categories = new ArrayCollection() ; 
    foreach($this->user_categories as $reference) { 
     $categories->add($reference->getCategory()) ; 
    } 
    return $categories ; 
} 

如果你不喜歡這樣,你可能會解決你的問題。

+0

我的問題現在已修復。我在參考表中使用了一個ID。所以我沒有兩個標識符了。而且我沒有使用Symfony,而是使用Zend框架2(標題)。 UserCategory實體從Category實體擴展,因爲會有更多的類別(內容,用戶,角色)。還是謝謝你的回答。 – Haidy 2013-05-14 14:20:26

相關問題