[英]Symfony FormType PropertyAccessor.php Error
I am getting an error when saving data using a formType into a model that has a many to many relationship. 使用formType将数据保存到具有多对多关系的模型中时,出现错误。
The error is 错误是
Catchable Fatal Error: Argument 1 passed to AppBundle\\Entity\\User::removeRole() must be an instance of AppBundle\\Entity\\Role, string given, called in /Code/project/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 616 and defined
可捕获的致命错误:传递给AppBundle \\ Entity \\ User :: removeRole()的参数1必须是AppBundle \\ Entity \\ Role的实例,给定字符串,在/ Code / project / vendor / symfony / symfony / src / Symfony / Component中调用/PropertyAccess/PropertyAccessor.php在第616行并已定义
What is the best way to inject the Role model into the form? 将角色模型注入表单的最佳方法是什么?
The model has the following: 该模型具有以下特征:
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
* @ORM\JoinTable(name="user_roles")
*/
private $roles;
/**
* Remove role
*
* @param Role $role
*/
public function removeRole(Role $role)
{
$this->roles->removeElement($role);
}
/**
* Add role
*
* @param Role $role
*
* @return User
*/
public function addRole(Role $role)
{
$this->roles[] = $role;
return $this;
}
There is the reverse in Role 角色相反
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*/
private $users;
To save the data in the controller I have 要将数据保存在我拥有的控制器中
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if( $form->isSubmitted() && $form->isValid() ){
$encryptedPassword = $this->get('security.password_encoder')
->encodePassword($user, $user->getPlainPassword());
$user->setPassword($encryptedPassword);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirect($this->generateUrl('user_list'));
}
return $this->render('user/new.html.twig', array('form' => $form->createView()));
And the FormType has 并且FormType具有
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('email')
->add('username')
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'invalid_message' => 'The password fields must match.',
'options' => array('attr' => array('class' => 'password-field')),
'required' => true,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
))
->add('roles', EntityType::class, array(
'class' => 'AppBundle:Role',
'choice_label' => 'name',
'multiple' => true
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\User'
));
}
}
I have worked out and it was a little gotcha that was the issue is and may cause others the same. 我已经解决了,这是一个小问题,可能会导致其他问题。
It is because: 这是因为:
class User implements UserInterface
which requires 这需要
/**
* @return mixed
*/
public function getRoles()
{
return ARRAY|STRING;
}
To get around this I create: 为了解决这个问题,我创建了:
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
* @ORM\JoinTable(name="user_roles")
*/
private $userRoles;
Everything then comes of getUserRoles and works fine. 然后,所有内容都来自getUserRoles并运行正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.