Auto create date - annoation - Codeigniter 2 and Doctrine 2
If i am wrong, correct me please. But i knew as if Doctrine does not support default. You do that in php level like
/** * @Column(type="string", length=255) */private $something = "blabla";
Looking at your source code, i see that you are using gedmo extension for doctrine. Am i right? So you have got two ways to do this.
1)Just using Doctrine, No Gedmo
Read this manual very carefully and you will notice @HasLifecycleCallbacks Annotations.
So you should edit your code as;
CLASS FILE
<?php namespace models; /** * @Entity * @Table(name="workers") * @HasLifecycleCallbacks */class Workers { /** * @Id * @Column(type="integer", nullable=false) * @GeneratedValue(strategy="AUTO") */ protected $id; /** * @Column(type="string", length=255, unique=true, nullable=false) */ protected $email; /** * @var datetime $created_on * @Column(type="datetime") */protected $created_on; /** @PrePersist */ function onPrePersist() { //using Doctrine DateTime here $this->created_on = new \DateTime('now'); } /* Setters & Getters */ public function setEmail($email){ $this->email = $email; } public function getEmail(){ return $this->email; }}
2)Using Gedmo
If you prefer using Gedmo Timestampable extension, then just drop the function prepersist, cause gedmo is doing everything for you. I also checked my source codes. I hope i do not have wrong predictions here
CLASS FILE
<?php namespace models; /** * @Entity * @Table(name="workers") */class Workers { /** * @Id * @Column(type="integer", nullable=false) * @GeneratedValue(strategy="AUTO") */ protected $id; /** * @Column(type="string", length=255, unique=true, nullable=false) */ protected $email; /** * @var datetime $created_on * @Column(type="datetime") * @gedmo:Timestampable(on="create") */protected $created_on; /* Setters & Getters */ public function setEmail($email){ $this->email = $email; } public function getEmail(){ return $this->email; }}
For doctrine 2.3, use
/** * @var datetime $created_on * * @Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"}) */protected $created_on;
You may try the followings:
/** * @var timestamp $created_on * * @Column(type="timestamp", default="CURRENT_TIMESTAMP") */protected $created_on;