3

我有以下具有多对多关系的实体: 用户

 /**
  * @ORM\ManyToMany(targetEntity = "Role", mappedBy = "users")
  *
  * @var ArrayCollection $roles;
  */
 private $roles;

角色

 /**
  * @ORM\ManyToMany(targetEntity = "User", inversedBy = "roles")
  * 
  * @var ArrayCollection $users
  */
 protected $users;

Doctrine 生成了 3 个表:用户角色 user_role

然后我添加了数据夹具:

class FixtureLoader implements FixtureInterface {
     public function load(ObjectManager $manager) {

          $role = new Role();
          $role->setName('Администратор');
          $role->setRole('ROLE_ADMIN');

          $manager->persist($role);

          $user = new User();
          $user->setUsername('ahmed');
          $user->setEmail('ahmed@umahanov.com');
          $user->setSalt(md5(time()));
          $user->setIsActive(false);

          $encoder = new MessageDigestPasswordEncoder('sha512',true,10);
          $password = $encoder->encodePassword('admin', $user->getSalt());
          $user->setPassword($password);
          //$user->getUserRoles()->add($role);
          $user->addRole($role);
          $manager->persist($user);

          $manager->flush();  

     }
}

当我使用控制台加载它时,只有 2 个表填充了数据,但保留多对多关系的 user_role 表是空的。

如何解决这个问题?还是我需要自己填充?

/**
 * Umahanov\UserBundle\Entity\User
 *
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User implements UserInterface, \Serializable {

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

     /**
      * @ORM\ManyToMany(targetEntity = "Role", mappedBy = "users")
      * @ORM\JoinTable(name="user_role",
      *   joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
      *   inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
      *   )
      * @var ArrayCollection $roles;
      */
     private $roles;

     /**
      * @var string
      *
      * @ORM\Column(name="username", type="string", length=25, unique=true)
      */
     private $username;

     /**
      * @var string
      *
      * @ORM\Column(name="salt", type="string", length=32)
      */
     private $salt;

     /**
      * @var string
      *
      * @ORM\Column(name="password", type="string", length=88)
      */
     private $password;

     /**
      * @var string
      *
      * @ORM\Column(name="email", type="string", length=60, unique=true)
      */
     private $email;

     /**
      * @var boolean
      *
      * @ORM\Column(name="is_active", type="boolean")
      */
     private $isActive;

     public function __construct() {
          $this->isActive = false;
          $this->salt = md5(uniqid(null, true));
          $this->roles = new ArrayCollection();
     }

     /**
      * Get roles (array)
      *
      * @return array
      */
     public function getRoles() {
          return $this->roles->toArray();
     }

     /**
      * @inheritDoc
      */
     public function eraseCredentials() {

     }

     /**
      * @see \Serializable::serialize()
      */
     public function serialize() {
          return serialize(array(
                      $this->id,
                  ));
     }

     /**
      * @see \Serializable::unserialize()
      */
     public function unserialize($serialized) {
          list (
                  $this->id,
                  ) = unserialize($serialized);
     }

     /**
      * Get id
      *
      * @return integer
      */
     public function getId() {
          return $this->id;
     }

     /**
      * Set username
      *
      * @param  string $username
      * @return User
      */
     public function setUsername($username) {
          $this->username = $username;

          return $this;
     }

     /**
      * Get username
      *
      * @return string
      */
     public function getUsername() {
          return $this->username;
     }

     /**
      * Set salt
      *
      * @param  string $salt
      * @return User
      */
     public function setSalt($salt) {
          $this->salt = $salt;

          return $this;
     }

     /**
      * Get salt
      *
      * @return string
      */
     public function getSalt() {
          return $this->salt;
     }

     /**
      * Set password
      *
      * @param  string $password
      * @return User
      */
     public function setPassword($password) {
          $this->password = $password;

          return $this;
     }

     /**
      * Get password
      *
      * @return string
      */
     public function getPassword() {
          return $this->password;
     }

     /**
      * Set email
      *
      * @param  string $email
      * @return User
      */
     public function setEmail($email) {
          $this->email = $email;

          return $this;
     }

     /**
      * Get email
      *
      * @return string
      */
     public function getEmail() {
          return $this->email;
     }

     /**
      * Set is_active
      *
      * @param  boolean $isActive
      * @return User
      */
     public function setIsActive($isActive) {
          $this->isActive = $isActive;

          return $this;
     }

     /**
      * Get is_active
      *
      * @return boolean
      */
     public function getIsActive() {
          return $this->isActive;
     }

     /**
      * Сравнивает пользователя с другим пользователем и определяет
      * один и тот же ли это человек.
      * 
      * @param UserInterface $user The user
      * @return boolean True if equal, false othwerwise.
      */
     public function equals(UserInterface $user) {
          return md5($this->getUsername()) == md5($user->getUsername());
     }

     /**
      * Add roles
      *
      * @param \Umahanov\UserBundle\Entity\Role $roles
      * @return User
      */
     public function addRole(\Umahanov\UserBundle\Entity\Role $roles) {
          $this->roles[] = $roles;

          return $this;
     }

     /**
      * Remove roles
      *
      * @param \Umahanov\UserBundle\Entity\Role $roles
      */
     public function removeRole(\Umahanov\UserBundle\Entity\Role $roles) {
          $this->roles->removeElement($roles);
     }

}

这是角色:

/**
 * @ORM\Entity
 * @ORM\Table(name="role")
 */
class Role implements RoleInterface {

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

     /**
      * @ORM\Column(type="string", length=255)
      * 
      * @var string $name
      */
     protected $name;

     /**
      * @ORM\Column(type="string", length=255)
      * 
      * @var string $role
      */
     protected $role;     

     /**
      * @ORM\Column(type="datetime", name="created_at")
      *
      * @var DateTime $createdAt;
      */
     protected $createdAt;

     public function getRole() {

          return $this->role;
     }

     /**
      * @ORM\ManyToMany(targetEntity = "User", inversedBy = "roles")
      * 
      * @var ArrayCollection $users
      */
     protected $users;


     public function getCreatedAt() {
          return $this->createdAt;
     }

     public function __construct() {
          $this->createdAt = new \DateTime();
          $this->users = new ArrayCollection();
     }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Role
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set role
     *
     * @param string $role
     * @return Role
     */
    public function setRole($role)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return Role
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Add users
     *
     * @param \Umahanov\UserBundle\Entity\User $users
     * @return Role
     */
    public function addUser(\Umahanov\UserBundle\Entity\User $users)
    {
        $this->users[] = $users;

        return $this;
    }

    /**
     * Remove users
     *
     * @param \Umahanov\UserBundle\Entity\User $users
     */
    public function removeUser(\Umahanov\UserBundle\Entity\User $users)
    {
        $this->users->removeElement($users);
    }

    /**
     * Get users
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getUsers()
    {
        return $this->users;
    }
}
4

2 回答 2

7

设置相关实体​​时,必须在两侧设置: 对于User类:

 /**
  * Add roles
  *
  * @param \Umahanov\UserBundle\Entity\Role $roles
  * @return User
  */
 public function addRole(\Umahanov\UserBundle\Entity\Role $roles) {
      $this->roles[] = $roles;
      $roles->addUser($this);

      return $this;
 }
于 2013-05-19T17:57:02.690 回答
2

我以稍微不同的方式来做这件事,它对我有用。
我有一个文件RoleGroups在我的情况下称为),如下所示:

<?php

class LoadGroupsData extends AbstractFixture implements OrderedFixtureInterface
{
    public function load(ObjectManager $manager)
    {
        foreach($this->container->getParameter('my_groups') as $key => $value)
        {
            $group = new Groups();
            $group->setRole($key);
            $group->setName($value);

            $manager->persist($group);
            $manager->flush();

            $this->addReference($key, $group);
        }
    }

    public function getOrder()
    {
        return 1;
    }
}

一个文件Users如下:

<?php

class LoadUsersData extends AbstractFixture implements OrderedFixtureInterface
{

    public function load(ObjectManager $manager)
    {
        foreach($this->container->getParameter('my_users') as $key => $value)
        {
            $user = new Users();
            $user->setUsername($key);

            $encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
            $user->setPassword($encoder->encodePassword($value, $user->getSalt()));

            $user->addRole($this->getReference('ROLE_ADMIN'));

            $manager->persist($user);
        }
        $manager->flush();
    }

    public function getOrder()
    {
        return 2;
    }
}

我的固定装置按顺序进行:Groups持久化和刷新,并且对象存储在固定装置之间共享的对象中。然后Users创建并附加到Groups存储在共享对象中。

因此,与您的主要区别在于我的Groups对象(Role在您的情况下)在尝试将它们设置给用户之前被持久化和刷新。

于 2013-05-19T15:24:07.007 回答