Replace an object with Null value Using Form Builder in Symfony2

百般思念 提交于 2019-12-11 06:48:47

问题


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

It throws the following error.

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

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);
                }
            }
        }

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.

Thanks in advance.


回答1:


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;
}

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



来源:https://stackoverflow.com/questions/10241333/replace-an-object-with-null-value-using-form-builder-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!