简体   繁体   中英

Edit entity field to null using Symfony2

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'))

// ...

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.

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;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM