自动创建日期 - annoation - Codeigniter 2和Doctrine 2

Puz*_*uzo 4 datetime automation doctrine annotations codeigniter

我正在使用Doctrine 2和Codeigniter 2,我想Doctrine自动生成表格中给定字段中插入的当前日期.

课程文件:

<?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; }
}
Run Code Online (Sandbox Code Playgroud)

插入方法:

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

每次我在表"workers"中插入新记录时,都会有allways created_on字段NULL而不是插入日期.我究竟做错了什么?

Mur*_*nal 11

如果我错了,请纠正我.但我知道,如果Doctrine不支持默认.你在php级别那样做

/**
 * @Column(type="string", length=255)
 */
private $something = "blabla";
Run Code Online (Sandbox Code Playgroud)

查看您的源代码,我看到您正在使用gedmo扩展进行doctrine.我对吗?所以你有两种方法可以做到这一点.

1)只使用Doctrine,No Gedmo 非常仔细
阅读本手册,您会注意到@HasLifecycleCallbacks Annotations.

所以你应该编辑你的代码;

课程文件

<?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; }
}
Run Code Online (Sandbox Code Playgroud)

2)使用Gedmo

如果你更喜欢使用Gedmo Timestampable扩展,那么只需删除函数prepersist,因为gedmo正在为你做所有事情.我还检查了我的源代码.我希望我在这里没有错误的预测

课程文件

<?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; }
}
Run Code Online (Sandbox Code Playgroud)


ihs*_*san 11

对于学说2.3,请使用

/**
 * @var datetime $created_on
 * 
 * @Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"})
 */
protected $created_on;
Run Code Online (Sandbox Code Playgroud)