与参考表的两个一对多关系(Doctrine 2,ZF2)

时间:2013-05-14 13:03:31

标签: doctrine-orm zend-framework2 entity-relationship

我的多对多关系存在问题。我想访问查询生成器查询的引用表。由于多对多关系,我无法访问我的参考表,因此我设置了两个一对多关系。我的结构看起来像:

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在该类别中具有“null”值。

我的用户实体:

/**
 * 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-entity:

/**
 * 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:

/**
 * 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-&gt; getEntityManager() - &gt; find('User \ Entity \ User','49') - &gt; 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 个答案:

答案 0 :(得分:1)

没有冒犯,但我觉得你的代码很难阅读。我建议你做一些修正,这甚至可以帮助你解决问题。

1:命名:代替UserCategory,将其重命名为Category。如果您的类别具有不同类型,请使用来自

等常量的值创建新列“type”
 class Category
 {
    const TYPE_USER = 1 ;
    ....

2:代替addCategories(Collection $ array),执行像

这样的单数版本
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自动识别这样的方法。即使您的关系是复数(如类别),s2也会找到奇异的addCategory和removeCategory方法。

要获取类别数组,请使用:

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

如果你这样做,你可能会解决你的问题。