Shrujan Shetty
Shrujan Shetty

Reputation: 2412

Replace an object with Null value Using Form Builder in Symfony2

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.

Upvotes: 1

Views: 2881

Answers (1)

gremo
gremo

Reputation: 48487

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.

Upvotes: 3

Related Questions