用Null值替换对象在Symfony2中使用Form Builder [英] Replace an object with Null value Using Form Builder in Symfony2

查看:142
本文介绍了用Null值替换对象在Symfony2中使用Form Builder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦坚持已经存在一个对象的null。

I am having trouble persisting a null which is already persisted with an object.

它会引发以下错误。

Catchable Fatal Error: Argument 1 passed to MyProject\EntityBundle\Entity\Requirements::setReplacedEmployee() must be an instance of MyProject\EntityBundle\Entity\Employee, null given, called in /var/www/MyProject/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 347 and defined in /var/www/MyProject/src/MyProject/EntityBundle/Entity/Requirements.php line 384

最初我保存replaceEmployee对象,该对象可能为null / object。但是后来如果我在编辑时用空格替换对象,会抛出上面的错误。

Initially i save the replacedEmployee object which could be null/object. But later on if i replace the object with a null while editing it throws the above error.

下面是我的控制器代码。

The below is my code from the controller.

try {
            if ($request->request->get('save') === 'Save') {

                $form->bindRequest($request); // this is the line which throws the above error

                if ($form->isValid()) {
                    $requirementObj->setUpdatedAt(new \DateTime('now'));
                    $em->flush();
                    $request->request->set('requirementId', $requirementId);
                    return $this->displayAction($request);
                }
            }
        }

这是需求中的内容.php是一个实体文件。

This is the content in Requirements.php which is an entity file.

 /**
 * @var replacedEmployee
 *
 * @ORM\ManyToOne(cascade={"persist"},targetEntity="Employee")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="replaced_employee_id",referencedColumnName="id",onDelete="CASCADE")
 * })
 */
private $replacedEmployee;


 /**
 * Set replacedEmployee
 *
 * @param MyProject\EntityBundle\Entity\Employee $replacedEmployee
 */
public function setReplacedEmployee(\MyProject\EntityBundle\Entity\Employee $replacedEmployee)
{
    $this->replacedEmployee = $replacedEmployee;
}

/**
 * Get replacedEmployee
 *
 * @return MyProject\EntityBundle\Entity\Employee 
 */
public function getReplacedEmployee()
{
    return $this->replacedEmployee;
}

任何人都可以提出解决此问题的解决方案。

Can anybody Suggest a solution to this problem.

提前感谢

推荐答案

我不能完全理解你的问题,但如果你想要允许与 Employee 的关系的 null 值,您应该首先编辑映射(这可能不是必需的,因为 JoinColumn 应该允许 null 的值默认):

I can't fully understand your question but, if you want to allow null values for the relation with Employee you should first edit the mapping (this maybe not necessary as JoinColumn should allow null values by default):

 /**
 * @var replacedEmployee
 *
 * @ORM\ManyToOne(cascade={"persist"},targetEntity="Employee")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(
 *     name="replaced_employee_id",referencedColumnName="id",onDelete="CASCADE",
 *     nullable=true
 *   )
 * })
 */
private $replacedEmployee;



<

After generating setters/getters Doctrine2 (if i remember correctly, starting from 2.2.1) should generate:

 /**
 * Set replacedEmployee
 *
 * @param MyProject\EntityBundle\Entity\Employee $replacedEmployee
 */
public function setReplacedEmployee(Employee $replacedEmployee = null)
{
    $this->replacedEmployee = $replacedEmployee;
}

请注意,参数是可选的(具有 null 默认值)。希望这有帮助。

Note that argument is optional (has null default value). Hope this helps.

这篇关于用Null值替换对象在Symfony2中使用Form Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆