Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Symfony2将实体字段编辑为空_Php_Symfony - Fatal编程技术网

Php 使用Symfony2将实体字段编辑为空

Php 使用Symfony2将实体字段编辑为空,php,symfony,Php,Symfony,如果该字段以前有值,则将实体字段编辑为NULL时出错。对于第一次持久化到数据库,我可以用空值提交它。将值更改回NULL时发生此错误: 可捕获致命错误:参数1传递给 Sifo\SharedBundle\Entity\BlogMenu::setBlogPage()必须是实例 Sifo\SharedBundle\Entity\BlogPage的,给定null,在中调用 C:\Sifony\vendor\symfony\symfony\src\symfony\Component\PropertyAcce

如果该字段以前有值,则将实体字段编辑为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行

这是我的entity 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'))

// ...

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

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

    return $this;
}

将您的代码修改为

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

    return $this;
}
正如我在评论中告诉你的,这是由于。类型暗示用于检查传递给函数的参数的类型(类;您可以检查的不是基元类型)。如果要让函数接受
null
类型,则应指定它


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

在我的例子中,问题出现在
php.ini
max\u input\u vars
变量中。默认情况下是
1000
,但我发送了3k+

这两种想法都不好:允许null并删除声明

这将解决问题,但不是类型safe@DonCallisto:你能建议一个安全的方法吗?