Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto create date - annoation - Codeigniter 2 and Doctrine 2

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?

like image 256
Puzo Avatar asked Nov 28 '22 17:11

Puzo


1 Answers

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; }
}
like image 60
Murat Ünal Avatar answered Dec 06 '22 09:12

Murat Ünal