php - Symfony forms: converting an ID back to an Entity before persisting to database -


i have user entity , role entity, many-to-many mapping between them. when displaying html form handles creation of new user, list available roles can assigned user per following code fragment (i've implemented __tostring() function correctly display role).

->add('roles', 'entity', array(                 'class'     => 'acme\userbundle\entity\role',                 'expanded'  => true,                 'multiple'  => true,             )) 

this works, @ least extent correctly displays available roles along checkboxes select roles should assigned user. the value of each checkbox refers id of role.

however, when select 1 of these checkboxes , try save form, receive following error:

this value should of type acme\userbundle\entity\role. 

clearly, problem i'm submitting ids of roles, , somewhere in php code need take ids , convert them actual entities, instead of trying persist ids themselves. how , do this?

for completeness, i've included current code below:



user.php

/**  * @orm\table(name="user")  * @orm\entity(repositoryclass="acme\userbundle\entity\userrepository")  */ class user implements advanceduserinterface {     /**      * @orm\column(type="guid")      * @orm\id      * @orm\generatedvalue(strategy="uuid")      */     private $id;      /**      * @orm\column(type="string", length=25, unique=true)      */     private $username;      /**      * @orm\column(type="string", length=64)      */     private $password;      /**      * @orm\manytomany(targetentity="role", inversedby="users")      * @orm\jointable(name="user_role")      * @assert\type(type="acme\userbundle\entity\role")      */     private $roles;      public function __construct()     {         $this->roles = new arraycollection();     }      /**      * @inheritdoc      */     public function getusername()     {         return $this->username;     }      /**      * @inheritdoc      */     public function getpassword()     {         return $this->password;     }      /**      * @inheritdoc      */     public function getroles()     {         return $this->roles->toarray();     }      /**      * @inheritdoc      */     public function erasecredentials()     {     }      /**      * @inheritdoc      */     public function isaccountnonexpired()     {         return true;     }      /**      * @inheritdoc      */     public function isaccountnonlocked()     {         return true;     }      /**      * @inheritdoc      */     public function iscredentialsnonexpired()     {         return true;     }      /**      * @inheritdoc      */     public function isenabled()     {         return true;     }      /**      * id      *      * @return guid      */     public function getid()     {         return $this->id;     }      /**      * set username      *      * @param string $username      * @return user      */     public function setusername($username)     {         $this->username = $username;          return $this;     }      /**      * set password      *      * @param string $password      * @return user      */     public function setpassword($password)     {         $this->password = $password;          return $this;     }      /**      * add roles      *      * @param role $roles      * @return user      */     public function addrole(role $roles)     {         $this->roles[] = $roles;          return $this;     }      /**      * remove roles      *      * @param role $roles      */     public function removerole(role $roles)     {         $this->roles->removeelement($roles);     } } 



role.php

<?php  /**  * @orm\table(name="role")  * @orm\entity()  */ class role implements roleinterface {     /**      * @orm\column(name="id", type="guid")      * @orm\id()      * @orm\generatedvalue(strategy="uuid")      */     private $id;      /**      * @orm\column(name="name", type="string", length=30)      */     private $name;      /**      * @orm\column(name="role", type="string", length=20, unique=true)      */     private $role;      /**      * @orm\manytomany(targetentity="user", mappedby="roles")      * @orm\jointable(name="user_role")      */     private $users;      public function __construct()     {         $this->users = new arraycollection();     }      public function __tostring()     {         return $this->name;     }      /**      * @see roleinterface      */     public function getrole()     {         return $this->role;     }      /**      * id      *      * @return guid      */     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;     }      /**      * add users      *      * @param user $users      * @return role      */     public function adduser(user $users)     {         $this->users[] = $users;          return $this;     }      /**      * remove users      *      * @param user $users      */     public function removeuser(user $users)     {         $this->users->removeelement($users);     }      /**      * users      *      * @return \doctrine\common\collections\collection       */     public function getusers()     {         return $this->users;     } } 



usertype.php

class usertype extends abstracttype {     /**      * @param formbuilderinterface $builder      * @param array $options      */     public function buildform(formbuilderinterface $builder, array $options)     {         $builder             ->add('username', 'text', array(                 'label'     => 'username'             ))             ->add('password', 'password', array(                 'label'     => 'password'             ))             ->add('email', 'email', array(                 'label'     => 'email address'             ))             ->add('roles', 'entity', array(                 'label'     => 'roles',                 'class'     => 'acme\userbundle\entity\role',                 'property'  => 'name',                 'expanded'  => true,                 'multiple'  => true,             ))         ;     }      /**      * @param optionsresolverinterface $resolver      */     public function setdefaultoptions(optionsresolverinterface $resolver)     {         $resolver->setdefaults(array(             'data_class' => 'acme\userbundle\entity\user'         ));     }      /**      * @return string      */     public function getname()     {         return 'acme_userbundle_user';     } } 



userrepository.php

/**  * userrepository  *  * class generated doctrine orm. add own custom  * repository methods below.  */ class userrepository extends entityrepository implements userproviderinterface {     /**      * @inheritdoc      */     public function loaduserbyusername($username)     {         $q = $this             ->createquerybuilder('u')             ->where('u.username = :username or u.email = :email')             ->setparameter('username', $username)             ->setparameter('email', $username)             ->getquery();          try {             // query::getsingleresult() method throws exception             // if there no record matching criteria             $user = $q->getsingleresult();         }         catch (noresultexception $e) {             $message = sprintf(                 'unable find active admin acmeuserbundle:user object identified "%s".',                 $username             );             throw new usernamenotfoundexception($message, 0, $e);         }          return $user;     }      /**      * @inheritdoc      */     public function refreshuser(userinterface $user)     {         $class = get_class($user);         if (!$this->supportsclass($class)) {             throw new unsupporteduserexception(                 sprintf(                     'instances of %s not supported',                     $class                 )             );         }          return $this->find($user->getid());     }      /**      * @inheritdoc      */     public function supportsclass($class)     {         return $this->getentityname() === $class             || is_subclass_of($class, $this->getentityname());     } } 

in user.php, above $roles variable, following assertion should removed:

@assert\type(type="acme\userbundle\entity\role") 

the $roles variable in fact array, , not of type role, hence validation failed.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -