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

发布于 2024-12-13 05:16:43 字数 1182 浏览 1 评论 0原文

我将 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 而不是插入日期。我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还在原地等你 2024-12-20 05:16:43

如果我错了,请纠正我。但我知道 Doctrine 似乎不支持默认。您在 php 级别执行此操作,例如

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

查看您的源代码,我发现您正在使用 gedmo 扩展来实现学说。我说得对吗?所以你有两种方法可以做到这一点。

1)只使用Doctrine,不使用Gedmo

仔细阅读本手册仔细观察,你会注意到 @HasLifecycleCallbacks 注解。

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

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)使用 Gedmo

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

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; }
}

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; }
}
怪异←思 2024-12-20 05:16:43

对于原则 2.3,使用

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

For doctrine 2.3, use

/**
 * @var datetime $created_on
 * 
 * @Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"})
 */
protected $created_on;
悟红尘 2024-12-20 05:16:43

您可以尝试以下方法:

/**
 * @var timestamp $created_on
 * 
 * @Column(type="timestamp", default="CURRENT_TIMESTAMP")
 */
protected $created_on;

You may try the followings:

/**
 * @var timestamp $created_on
 * 
 * @Column(type="timestamp", default="CURRENT_TIMESTAMP")
 */
protected $created_on;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文