dongpao9165 2014-10-06 06:21
浏览 33
已采纳

使用Symfony2将实体字段编辑为null

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

// ...
  • 写回答

2条回答 默认 最新

  • drhozgt6007 2014-10-06 07:58
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?