我无法持久化已与对象一起持久化的空值。
它引发以下错误。
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
最初我保存可能是空/对象的replacedEmployee对象。但后来如果我在编辑时用 null 替换对象,则会引发上述错误。
以下是我来自控制器的代码。
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);
}
}
}
这是Requirements.php 中的内容,它是一个实体文件。
/**
* @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;
}
任何人都可以提出解决此问题的方法。
提前致谢。