T
T
tsifra2013-11-30 22:25:37
symfony
tsifra, 2013-11-30 22:25:37

How to assign null to a field in doctrine2?

Today I encountered the fact that doctrine does not save fields that are assigned null
For example
..
$object->setLockedBy(null);
$object->setLockedAt(null);
..
$em->persist($object);
$em->flush();
As a result, the value of the specified fields does not change. getter and setter standard generated by symfony2

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Gusakov, 2013-12-01
@hell0w0rd

Everything works.
Show all the code, where do you get the object from, what config does the entity have

T
tsifra, 2013-12-01
@tsifra

<?php

namespace Backend\WorkorderBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

use Doctrine\ORM\Mapping as ORM;

/**
 * Workorder
 *
 * @ORM\Table(name="workorder")
 * @ORM\Entity(repositoryClass="Backend\WorkorderBundle\Entity\WorkorderRepository")
 */
class Workorder
{
//..
    /**
     * @var integer
     *
     * @ORM\Column(name="locked_by", type="integer", nullable=true)
     */
    private $lockedBy;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="locked_at", type="datetime", nullable=true)
     */
    private $lockedAt;
//..
    public function __construct()
    {
        //здесь про эти два объекта ничего
    }
//..
    /**
     * Set lockedBy
     *
     * @param integer $lockedBy
     * @return Workorder
     */
    public function setLockedBy($lockedBy)
    {
        $this->lockedBy = $lockedBy;
    
        return $this;
    }

    /**
     * Get lockedBy
     *
     * @return integer 
     */
    public function getLockedBy()
    {
        return $this->lockedBy;
    }
//..

if instead of null in this code some other values ​​are set, then the database is updated without any problems.
$object->setLockedBy(null);
$object->setLockedAt(null);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question