symfony forms - Symfony3: Database and Entity settings: Neither property … nor method …nor method … exists in class -
i know there similar questions, none of solutions work symfony 3.x.
so have 3 entities, user, role , permission. user has roles (manytomany) , permissions (also manytomany). , have form, , while permissions work fine, roles not.
the difference can see, function returning roles isn't called getroles should(?), because function name has return array of strings (not array of role entities) meet expectations of advanceduserinterface, why function (getroleentities) returns $this->roles, believe reflected while building form.
here shortened version of user entity class:
/** * @orm\table(name="user") * @orm\entity(repositoryclass="appbundle\repository\userrepository") */ class user implements advanceduserinterface, \serializable { //id, username, personalname, password, email , isactive ommited /** * * @orm\manytomany(targetentity="appbundle\entity\permission", cascade = {"persist"}) * @orm\jointable(name="user_permission") */ private $permissions;//this works /** * * @orm\manytomany(targetentity="appbundle\entity\role", cascade = {"persist"}) * @orm\jointable(name="user_role") */ private $roles;//this doesn't work public function __construct() { $this->permission = new arraycollection(); $this->roles = new arraycollection(); } //irrelevant getters/setters ommited /** * add permission * * @param \appbundle\entity\permission $userpermission * * @return user */ public function addpermission(\appbundle\entity\permission $userpermission) { $this->permissions[] = $userpermission; return $this; } /** * remove permission * * @param \appbundle\entity\permission $userpermission */ public function removepermission(\appbundle\entity\permission $userpermission) { $this->permissions->removeelement($userpermission); } /** * permissions * * @return \doctrine\common\collections\collection */ public function getpermissions() { return $this->permissions; } //////////////////////////////// //roles /** * add role * @param \appbundle\entity\role $role * * @return user */ public function addroleentity(\appbundle\entity\role $role) { $this->roles[] = $role; return $this; } /** * remove role * @param \appbundle\entity\role $role */ public function removeroleentity(\appbundle\entity\role $role) { $this->roleroles->removeelement($role); } /** * know 1 should return $this->roles, has return array of strings meet expectations of advanceduserinterface, why function (getroleentities) returns $this->roles * @return array */ public function getroles() { $ret_val = array(); $roles = $this->getroleentities(); if ($roles) { foreach ($roles $role) { $ret_val[] = $role->getrolename(); } } return $ret_val; } /** * roles */ public function getroleentities() { return $this->roles; } }
and here how build form:
$em = $this->getdoctrine()->getmanager(); $user = $em->find('appbundle:user',1);//just testing $form = $this->createformbuilder($user) ->add('personal_name', \symfony\component\form\extension\core\type\texttype::class, array('label' => 'imię nazwisko: ')) ->add('is_active', \symfony\component\form\extension\core\type\checkboxtype::class, array('label' => 'active: ','required' => false)) ->add('permissions', \symfony\bridge\doctrine\form\type\entitytype::class , array( 'class' => 'appbundle\entity\permission', 'multiple' => true, 'expanded' => true )) ->add('roleentities', \symfony\bridge\doctrine\form\type\entitytype::class , array( 'class' => 'appbundle\entity\role', )) ->add('save', \symfony\component\form\extension\core\type\submittype::class, array('label' => 'save')) ->getform();
and when i'm opening page exception:
neither property "roleentities" nor 1 of methods "addroleentity()"/"removeroleentity()", "setroleentities()", "roleentities()", "__set()" or "__call()" exist , have public access in class "appbundle\entity\user".
ps. tagged "symfony2", because afaik there no big differences between 2.9 , 3.0
ok, fortunately guys #symfony irc, helped me, there few issues there:
functions addroleentity should rather called after variable, e.g. addrole
like @malcom mentioned, while building form, instead of:
->add('roleentities', \symfony\bridge\doctrine\form\type\entitytype::class , array( 'class' => 'appbundle\entity\role',
it should be:
->add('roles', \symfony\bridge\doctrine\form\type\entitytype::class , array( 'class' => 'appbundle\entity\role', 'multiple' => true,
)
because how variable named. plus, since manytomany added 'multiple' => true, otherwise did save, incorectly loaded information.
3.it went further, , new error appeared: can not read choices choice list.
i changed body of getroles method to:
return $this->roles->toarray();
and worked.
the last idea mine, not irc, i'm saying this, because don't solution. if bad solution, it's fault.
Comments
Post a Comment