I got an error when editing Entity field to NULL if this field had value before. For first time persisting to database, I can submit it with NULL value. This error occured when change the value back to NULL :
Catchable Fatal Error: Argument 1 passed to Sifo\SharedBundle\Entity\BlogMenu::setBlogPage() must be an instance of Sifo\SharedBundle\Entity\BlogPage, null given, called in C:\Sifony\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php on line 438 and defined in C:\Sifony\src\Sifo\SharedBundle\Entity\BlogMenu.php line 332
This is my entity BlogMenu.php Line 332 :
// ...
* Set blogPage
*
* @param \Sifo\SharedBundle\Entity\BlogPage $blogPage
* @return blogPage
*/
public function setBlogPage(\Sifo\SharedBundle\Entity\BlogPage $blogPage) // Line 332
{
$this->blogPage = $blogPage;
return $this;
}
/**
* Get blogPage
*
* @return \Sifo\SharedBundle\Entity\BlogPage
*/
public function getBlogPage()
{
return $this->blogPage;
}
// ...
updateAction in my controller is like this :
/**
* Edits an existing BlogMenu entity.
*
*/
public function updateAction(Request $request, $id)
{
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('SifoSharedBundle:BlogMenu')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find BlogMenu entity.');
}
$entity->setOperator($user->getName());
$form = $this->createEditForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('admin_blog_menu_show', array('id' => $id)));
}
return $this->render('SifoAdminBundle:edit:layout.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
'user' => $user,
));
}
This is my FormType :
<?php
// ...
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('blog_page', 'entity', array(
'required' => false,
'empty_value' => 'admin.choice.choosePage',
'class' => 'Sifo\SharedBundle\Entity\BlogPage'))
// ...