【问题标题】:Edit entity field to null using Symfony2使用 Symfony2 将实体字段编辑为空
【发布时间】:2014-11-30 09:31:00
【问题描述】:

如果实体字段之前有值,则在将实体字段编辑为 NULL 时出现错误。第一次持久化到数据库,我可以用 NULL 值提交它。将值更改回 NULL 时发生此错误:

可捕获的致命错误:参数 1 传递给 Sifo\SharedBundle\Entity\BlogMenu::setBlogPage() 必须是一个实例 Sifo\SharedBundle\Entity\BlogPage,给定 null,调用 C:\Sifony\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php 在第 438 行并在 C:\Sifony\src\Sifo\SharedBundle\Entity\BlogMenu.php 第 332 行

这是我的实体 BlogMenu.php 第 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是这样的:

/**
 * 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,
    ));
}

这是我的表单类型:

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

// ...

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    把你的代码修改成这个

    public function setBlogPage(\Sifo\SharedBundle\Entity\BlogPage $blogPage = null) 
    {
        $this->blogPage = $blogPage;
    
        return $this;
    }
    

    正如我在评论中告诉你的,这是由于type hinting。类型提示用于检查传递给函数的参数的类型(类;不是原始类型,您可以检查this answer)。如果你想让你的函数接受null 类型,你应该指定它。

    最好使用类型提示,因为它“更安全”并且不会导致“副作用”(如果可能)。让我们考虑第三方库或供应商:如果您使用第三方库中的函数,您应该知道参数类型,如果您尝试传递错误的类型,“编译器”(解析器)会通知您。

    【讨论】:

      【解决方案2】:

      我不确定这是否是解决此问题的好方法。我在我的实体中删除了变量声明作为实体。

       * Set blogPage
       *
       * @param \Sifo\SharedBundle\Entity\BlogPage $blogPage
       * @return blogPage
       */
      public function setBlogPage($blogPage) // Line 332
      {
          $this->blogPage = $blogPage;
      
          return $this;
      }
      

      【讨论】:

      • 这将解决问题,但它不安全
      • @DonCallisto : 你能建议一个安全的方法吗?
      【解决方案3】:

      在我的情况下,prblem 位于 php.ini max_input_vars 变量中。默认是1000,但我发送了 3k+
      这两个想法都不好:允许 null 并删除声明

      【讨论】:

        猜你喜欢
        • 2012-11-10
        • 1970-01-01
        • 1970-01-01
        • 2012-05-07
        • 1970-01-01
        • 1970-01-01
        • 2014-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多