自动创建日期 - 烦恼 - Codeigniter 2和Doctrine 2 [英] Auto create date - annoation - Codeigniter 2 and Doctrine 2
问题描述
CLASS FILE:
<?php
命名空间模型;
/ **
* @Entity
* @Table(name =workers)
* /
class Workers {
/ **
* @Id
* @Column(type =integer,nullable = false)
* @GeneratedValue(strategy =AUTO)
* /
保护$ 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('Ymd 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();
每当我在表工作者中插入新记录时,都会有code> created_on 字段 NULL
而不是插入日期。我做错了什么?
如果我错了,请更正我。但我知道如果教义不支持默认。你这样做在php级别像
/ **
* @Column(type =string,length = 255)
* /
private $ something =blabla;
看看你的源代码,我看到你正在使用gedmo扩展的原则。我对吗?
1)只使用Doctrine,No Gedmo
读取< a href =http://www.doctrine-project.org/docs/orm/2.0/en/reference/annotations-reference.html =noreferrer>本手册非常仔细,你会注意到@HasLifecycleCallbacks注释。
所以你应该编辑你的代码;
CLASS FILE
<?php
命名空间模型;
/ **
* @Entity
* @Table(name =workers)
* @HasLifecycleCallbacks
* /
类工人{
/ **
* @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 * /
函数onPrePersist()
{
//使用Doctrine DateTime这里
$ 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
命名空间模型;
/ **
* @Entity
* @Table(name =workers)
* /
class Workers {
/ **
* @Id
* @Column(type =integer,nullable = false)
* @GeneratedValue(strategy =AUTO)
* /
保护$ 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; }
}
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?
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; }
}
这篇关于自动创建日期 - 烦恼 - Codeigniter 2和Doctrine 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!