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.
I can’t fully understand your question but, if you want to allow
nullvalues for the relation withEmployeeyou should first edit the mapping (this maybe not necessary asJoinColumnshould allownullvalues by default):After generating setters/getters Doctrine2 (if i remember correctly, starting from 2.2.1) should generate:
Note that argument is optional (has
nulldefault value). Hope this helps.