Doctrine 2.1 - datetime column default value

前端 未结 7 1273
小蘑菇
小蘑菇 2020-12-30 18:51

Could someone tell me how to add default value on datetime column? I can\'t do this like this:

protected $registration_date = date(\"Y-m-d H:i:s\", time());
         


        
7条回答
  •  清歌不尽
    2020-12-30 19:11

    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.

提交回复
热议问题