我正在为使用FOSUserBundle的Symfony3应用程序创建一个简单的控制器来更新用户的电子邮件地址。
$user = $this->getUser();
$user->setEmail($email);
在第二行返回此错误。
Error: Cannot use object of type AppBundle\Entity\User as array
我明显没有将对象用作数组,考虑到我使用的是BaseUser类的方法,所以我不知道为什么会抛出这个错误。
编辑:
控制器
/**
* @Route("/portfolio/settings", name="portfolio_settings")
*/
public function settingsAction(Request $request)
{
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$email_form = $this->get('form.factory')->createNamedBuilder('change_email', FormType::class, $user)
->add('email', EmailType::class, array(
'label' => 'E-Mail',
))
->add('save', SubmitType::class, array(
'label' => 'Save'
))
->getForm();
$password_form = $this->get('form.factory')->createNamedBuilder('change_password', FormType::class, $user)
->add('password', RepeatedType::class, [
'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('save', SubmitType::class)
->getForm();
$email_form->handleRequest($request);
$password_form->handleRequest($request);
if($email_form->isValid()){
$data = $email_form->getData();
$user->setEmail($data['email']);
$em->persist($user);
$this->addFlash(
'success',
'Your E-Mail address has been updated successfully'
);
}
$em->flush();
return $this->render('AppBundle:Portfolio:settings.html.twig', array(
'email' => $email_form->createView(),
'password' => $password_form->createView(),
));
}
堆栈跟踪
[1] Symfony\Component\Debug\Exception\FatalErrorException: Error: Cannot use object of type AppBundle\Entity\User as array
at n/a
in /var/www/html/scnce/src/AppBundle/Controller/PortfolioController.php line 114
第114行是
$user->setEmail($data['email']);
答案 0 :(得分:0)
$data = $email_form->getData();
$user->setEmail($data['email']);
Symfony \ Component \ Debug \ Exception \ FatalErrorException:错误:不能 使用AppBundle \ Entity \ User类型的对象作为数组
它说$data
是您的实体用户......
(抱歉,简答)
答案 1 :(得分:0)
扩展第一个答案。
在Symfony中,当您使用实体创建表单时,表单方法getData()
将返回最初传递给它的实体。因此,如果您已经拥有该实体(对象),则无需致电getData()
此外,handleRequest()
已根据您的身份更改了实体的值你的表格。除非在表单字段中您在选项中指定了'mapped' => false
。
实施例
/**
* @Route("/portfolio/settings", name="portfolio_settings")
*/
public function settingsAction(Request $request)
{
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$email_form = $this->get('form.factory')->createNamedBuilder('change_email', FormType::class, $user)
->add('email', EmailType::class, array(
'label' => 'E-Mail',
))
->add('save', SubmitType::class, array(
'label' => 'Save'
))
->getForm();
$email_form->handleRequest($request);
if($email_form->isValid()){
// Here we can persist the user, it's field for email have already been modified by ->handleRequest($request);
// In fact if you var_dump($user->getEmail()) you'll see it's already your new value.
$em->persist($user);
$this->addFlash(
'success',
'Your E-Mail address has been updated successfully'
);
// I moved your flush here, because it's more appropriate to only flush when we've actually made a change.
$em->flush();
}
return $this->render('AppBundle:Portfolio:settings.html.twig', array(
'email' => $email_form->createView(),
'password' => $password_form->createView(),
));
}
您可以在文档http://symfony.com/doc/current/book/forms.html
中找到所有这些以及更多内容最后$data = $form->getData()
返回了您的用户实体,因此$data['email']
正在向您提供错误。
希望这有帮助。