Reputation: 1884
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'))
// ...
Upvotes: 0
Views: 829
Reputation: 954
In my case prblem was in php.ini
max_input_vars
variable. It was 1000
by default but I sent 3k+
Both ideas are bad: allow null and remove declaration
Upvotes: 0
Reputation: 1884
I'm not sure if this is a good way to solve this problem. I removed the variable declaration as entity in my Entity.
* Set blogPage
*
* @param \Sifo\SharedBundle\Entity\BlogPage $blogPage
* @return blogPage
*/
public function setBlogPage($blogPage) // Line 332
{
$this->blogPage = $blogPage;
return $this;
}
Upvotes: 0
Reputation: 29912
Modify your code to this
public function setBlogPage(\Sifo\SharedBundle\Entity\BlogPage $blogPage = null)
{
$this->blogPage = $blogPage;
return $this;
}
As I told you in comment, this is due to type hinting. Type hinting is used to check type (classes; not primitive type, you can check this answer) of arguments passed to a function. If you want to let your function accept null
type you should specify it.
Is better to use type hinting as it is "safer" and will not cause "side effects" (if possible). Let's think about a third party library or vendor: if you use a function from a third party library you should know the parameter types and if you attempt to pass the wrong type, the "compiler" (parser) will notify you.
Upvotes: 1