symfony2 doctrine allow null values?

Johnny picture Johnny · Apr 3, 2012 · Viewed 77.5k times · Source

So, I'm still REALLY new to symfony but i'm learning quickly... I created an entity and a crud (i forget how I did it actually but it was via the command line)..

The entity was created with this in it:

namespace Ecs\CrmBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Ecs\CrmBundle\Entity\TimeClock
 */
class TimeClock
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var datetime $in1
     */
    private $in1;

    /**
     * @var datetime $out1
     */
    private $out1;

    /**
     * @var datetime $in2
     */
    private $in2;

    /**
     * @var datetime $out2
     */
    private $out2;

    /**
     * @var datetime $in3
     */
    private $in3;

    /**
     * @var datetime $out3
     */
    private $out3;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set in1
     *
     * @param datetime $in1
     * @return TimeClock
     */
    public function setin1($in1)
    {
        $this->in1 = $in1;
        return $this;
    }

    /**
     * Get in1
     *
     * @return datetime 
     */
    public function getin1()
    {
        return $this->in1;
    }

    /**
     * Set in2
     *
     * @param datetime $in2
     * @return TimeClock
     */
    public function setin2($in2)
    {
        $this->in2 = $in2;
        return $this;
    }

    /**
     * Get in2
     *
     * @return datetime 
     */
    public function getin2()
    {
        return $this->in2;
    }

    /**
     * Set in3
     *
     * @param datetime $in3
     * @return TimeClock
     */
    public function setin3($in3)
    {
        $this->in3 = $in3;
        return $this;
    }

    /**
     * Get in3
     *
     * @return datetime 
     */
    public function getin3()
    {
        return $this->in3;
    }

    /**
     * Set in4
     *
     * @param datetime $in4
     * @return TimeClock
     */
    public function setin4($in4)
    {
        $this->in4 = $in4;
        return $this;
    }

    /**
     * Get in4
     *
     * @return datetime 
     */
    public function getin4()
    {
        return $this->in4;
    }

    /**
     * Set in5
     *
     * @param datetime $in5
     * @return TimeClock
     */
    public function setin5($in5)
    {
        $this->in5 = $in5;
        return $this;
    }

    /**
     * Get in5
     *
     * @return datetime 
     */
    public function getin5()
    {
        return $this->in5;
    }

    /**
     * Set in6
     *
     * @param datetime $in6
     * @return TimeClock
     */
    public function setin6($in6)
    {
        $this->in6 = $in6;
        return $this;
    }

    /**
     * Get in6
     *
     * @return datetime 
     */
    public function getin6()
    {
        return $this->in6;
    }
    /**
     * @var Ecs\AgentManagerBundle\Entity\User
     */
    private $noteBy;


    /**
     * Set noteBy
     *
     * @param Ecs\AgentManagerBundle\Entity\User $noteBy
     * @return TimeClock
     */
    public function setNoteBy(\Ecs\AgentManagerBundle\Entity\User $noteBy = null)
    {
        $this->noteBy = $noteBy;
        return $this;
    }

    /**
     * Get noteBy
     *
     * @return Ecs\AgentManagerBundle\Entity\User 
     */
    public function getNoteBy()
    {
        return $this->noteBy;
    }
    /**
     * @var datetime $in1
     */
    private $in1;

    /**
     * @var datetime $out1
     */
    private $out1;

    /**
     * @var datetime $in2
     */
    private $in2;

    /**
     * @var datetime $out2
     */
    private $out2;

    /**
     * @var datetime $in3
     */
    private $in3;

    /**
     * @var datetime $out3
     */
    private $out3;

    /**
     * @var varchar $totaltime
     */
    private $totaltime;

    /**
     * @var datetime $daydate
     */
    private $daydate;


    /**
     * Set in1
     *
     * @param datetime $in1
     * @return TimeClock
     */
    public function setIn1($in1)
    {
        $this->in1 = $in1;
        return $this;
    }

    /**
     * Get in1
     *
     * @return datetime 
     */
    public function getIn1()
    {
        return $this->in1;
    }

    /**
     * Set out1
     *
     * @param datetime $out1
     * @return TimeClock
     */
    public function setOut1($out1)
    {
        $this->out1 = $out1;
        return $this;
    }

    /**
     * Get out1
     *
     * @return datetime 
     */
    public function getOut1()
    {
        return $this->out1;
    }

    /**
     * Set in2
     *
     * @param datetime $in2
     * @return TimeClock
     */
    public function setIn2($in2)
    {
        $this->in2 = $in2;
        return $this;
    }

    /**
     * Get in2
     *
     * @return datetime 
     */
    public function getIn2()
    {
        return $this->in2;
    }

    /**
     * Set out2
     *
     * @param datetime $out2
     * @return TimeClock
     */
    public function setOut2($out2)
    {
        $this->out2 = $out2;
        return $this;
    }

    /**
     * Get out2
     *
     * @return datetime 
     */
    public function getOut2()
    {
        return $this->out2;
    }

    /**
     * Set in3
     *
     * @param datetime $in3
     * @return TimeClock
     */
    public function setIn3($in3)
    {
        $this->in3 = $in3;
        return $this;
    }

    /**
     * Get in3
     *
     * @return datetime 
     */
    public function getIn3()
    {
        return $this->in3;
    }

    /**
     * Set out3
     *
     * @param datetime $out3
     * @return TimeClock
     */
    public function setOut3($out3)
    {
        $this->out3 = $out3;
        return $this;
    }

    /**
     * Get out3
     *
     * @return datetime 
     */
    public function getOut3()
    {
        return $this->out3;
    }

    /**
     * Set totaltime
     *
     * @param varchar $totaltime
     * @return TimeClock
     */
    public function setTotaltime(\varchar $totaltime)
    {
        $this->totaltime = $totaltime;
        return $this;
    }

    /**
     * Get totaltime
     *
     * @return varchar 
     */
    public function getTotaltime()
    {
        return $this->totaltime;
    }

    /**
     * Set daydate
     *
     * @param datetime $daydate
     * @return TimeClock
     */
    public function setDaydate($daydate)
    {
        $this->daydate = $daydate;
        return $this;
    }

    /**
     * Get daydate
     *
     * @return datetime 
     */
    public function getDaydate()
    {
        return $this->daydate;
    }
}

Then when I did i think the doctrine:generate:entities thing (i could be completely wrong -- i forget what i did to create it)..

It generates a timeclock.orm.yml and creates the table in the database or something like that..

the timeclock.orm.yml has this:

Ecs\CrmBundle\Entity\TimeClock:
    type: entity
    table: null
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
        in1:
            type: datetime
        out1:
            type: datetime
        in2:
            type: datetime
        out2:
            type: datetime
        in3:
            type: datetime
        out3:
            type: datetime
        totaltime:
            type: string
        daydate:
            type: datetime
    manyToOne:
        noteBy:
            targetEntity:  Ecs\AgentManagerBundle\Entity\User
    lifecycleCallbacks: {  }

The problem is, totaltime, and all the in and out fields need to have a default of NULL and I can't find how to do that.. I've been searching for the last 30 minutes trying to figure it out before I came here..

Then, once I edit whatever file(s) i need, how do I make it update it in the database too?

Answer

Daniel P picture Daniel P · May 27, 2012

I've just been looking for the solution, and wasn't happy with the accepted solution. [Edit: though I now know it's because I was using annotation]. The way I've found that works for me is setting nullable:

/**
 * @ORM\Column(type="varchar", nullable=true)
 */
private $totaltime = null;