问题描述
我使用学说2 $ C $ 2 cigniter,我想学说在表中某一领域自动生成插入当前日期。
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.
类文件:
<?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方法:
$worker = new models\Workers();
$worker->setEmail($data['email']);
$this->em->persist($worker);
$this->em->flush();
每次我插入新的记录表打工皇帝,有永诺 created_on
字段 NULL
而不是插入日期。我在做什么错了?
Everytime I insert new record in table "workers", there is allways created_on
field NULL
instead of insertion date. What am I doing wrong?
推荐答案
如果我错了,纠正我请。但我知道,如果学说不支持默认。你这样做,在PHP层像
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";
看你的源代码code,我看到你正在使用的学说gedmo扩展。我对吗?所以,你有两种方法可以做到这一点。
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)只要使用Doctrine,无Gedmo
阅读本手册会让你注意@HasLifecycleCallbacks注解。
1)Just using Doctrine, No Gedmo
Read this manual very carefully and you will notice @HasLifecycleCallbacks Annotations.
所以,你应该修改code作为;
So you should edit your code as;
类文件
<?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扩展preFER,那么就删除函数prepersist,原因gedmo为你做的一切。我还检查我的源$ C $ CS。我希望我没有错predictions此处
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
类文件
<?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; }
}
这篇关于自动创建日期 - Generated注释 - $ C $ 2 cigniter和Doctrine 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!