0

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

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;
     }
}

我的用户类别实体:

/**
 * 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;
     }
}

我的用户用户类别实体:

/**
 * 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 中的第一个输入,从第二个开始并且不能再获得最后一个类别。

4

1 回答 1

1

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

1:命名:代替UserCategory,将其重命名为Category。如果您的类别将具有不同的类型,请使用来自常量的值创建新列“类型”,例如

 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 ;
}

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

于 2013-05-14T13:54:58.007 回答