4

I am using Doctrine 2 with Codeigniter 2 and I would like to Doctrine automatically generate current date on insert in a given field in the table.

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
     * 
     * @gedmo:Timestampable(on="create")
     * @Column(type="datetime")
     */
    protected $created_on;


    /** @PrePersist */
    function onPrePersist()
    {
        $this->created_on = date('Y-m-d H:i:s');
    }

    /* Setters & Getters */
    public function setEmail($email){ $this->email = $email; }
    public function getEmail(){ return $this->email; }
}

INSERT METHOD:

$worker = new models\Workers();
$worker->setEmail($data['email']);
$this->em->persist($worker);
$this->em->flush();

Everytime I insert new record in table "workers", there is allways created_on field NULL instead of insertion date. What am I doing wrong?

3 Answers 3

11

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; }
}
2
  • Thank you for you solution. I solved without GEDMO extension.. Thank you!
    – Puzo
    Dec 10, 2011 at 9:03
  • I use this default: @ORM\Column(type="integer", options={"default":1})
    – woodo
    May 19, 2016 at 13:38
11

For doctrine 2.3, use

/**
 * @var datetime $created_on
 * 
 * @Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"})
 */
protected $created_on;
1

You may try the followings:

/**
 * @var timestamp $created_on
 * 
 * @Column(type="timestamp", default="CURRENT_TIMESTAMP")
 */
protected $created_on;
3
  • Thank you. I will try as soon as I'll solve my new problem - stackoverflow.com/questions/7985676/…
    – Puzo
    Nov 3, 2011 at 13:57
  • When I try your solution, there is still error - pastebin.com/pC313P0S. Any idea?
    – Puzo
    Nov 4, 2011 at 11:29
  • the type has to be datetime, although it will be applied as timestamp with default value CURRENT_TIMESTAMP... timestamp is just not a valid type!
    – MediaVince
    Apr 8, 2015 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.