symfony - Symfony2. Populate ManyToMany connection table using data fixtures -


i have following entities many many relation: user

 /**   * @orm\manytomany(targetentity = "role", mappedby = "users")   *   * @var arraycollection $roles;   */  private $roles; 

role

 /**   * @orm\manytomany(targetentity = "user", inversedby = "roles")   *    * @var arraycollection $users   */  protected $users; 

doctrine generated 3 tables: user role user_role

then added data fixture:

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

when load using console, 2 tables populated data, user_role table, keep manytomany relations empty.

how solve this? or need populate myself?

/**  * 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();      }       /**       * 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);      }       /**       * 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;      }       /**       * 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;      }       /**       * 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;      }       /**       * 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;      }       /**       * 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;      }       /**       * is_active       *       * @return boolean       */      public function getisactive() {           return $this->isactive;      }       /**       * Сравнивает пользователя с другим пользователем и определяет       * один и тот же ли это человек.       *        * @param userinterface $user 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);      }  } 

and here role:

/**  * @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();      }       /**      * 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;     }      /**      * 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);     }      /**      * users      *      * @return \doctrine\common\collections\collection       */     public function getusers()     {         return $this->users;     } } 

when set related entities, must set on both sides: user class:

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

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -