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

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动