Doctrine 2.1 - datetime column default value

Solution 1:

For default value CURRENT_TIMESTAMP:

     @ORM\Column(name="created_at", type="datetime", options={"default": "CURRENT_TIMESTAMP"})

Or for older Symfony versions:

     @ORM\Column(name="created_at", type="datetime", options={"default": 0})

Worked for me... However this works only with MySQL.

Solution 2:

You map your property as DateTime type then set the value in the constructor using a new DateTime instance:

/**
 * @Entity
 * @Table(name="...")
 */
class MyEntity
{
    /** @Column(type="datetime") */
    protected $registration_date;

    public function __construct()
    {
        $this->registration_date = new DateTime(); 
    }
}

This works as the constructor of a persisted class is not called upon hydration.