codeigniter - 自动创建日期 - 注释 - Codeigniter 2 和 Doctrine 2

标签 codeigniter datetime doctrine automation annotations

我正在将 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; }
}

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

每次我在表“workers”中插入新记录时,总是created_on领域NULL而不是插入日期。我究竟做错了什么?

最佳答案

如果我错了,请纠正我。但我知道好像 Doctrine 不支持默认值。你在 php 级别这样做

/**
 * @Column(type="string", length=255)
 */
private $something = "blabla";

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

1) 只使用 Doctrine,没有 Gedmo
阅读 this manual非常小心,您会注意到@HasLifecycleCallbacks 注释。

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

类文件
<?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) 使用 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; }
}

关于codeigniter - 自动创建日期 - 注释 - Codeigniter 2 和 Doctrine 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7938793/

相关文章:

php - ExpressionEngine no_results 不工作

database - 数据库驱动的 CodeIgniter Web 应用程序中的单元测试

java - 防止 MySQL JDBC 驱动程序将 DateTime 转换为本地时间

doctrine-orm - 如何将 migrations_paths 传递给 doctrine :migrations:migrate?

symfony1 - 将 memcache 设置为默认值,仅适用于 symfony 中的条令查询

php - 具有社交网站集成的最佳 codeigniter 2.1 身份验证库

php - CodeIgniter 3 - 上传文件错误: Undefined property/Call to a member function on null

SQL Server 日期时间格式

c# - 在 C#/.NET 中逐笔更新到烛台/OHLC 数据

php - doctrine dbal querybuilder作为准备好的语句