问题描述
如何将 Unix 时间戳与 Doctrine Timestampable 行为结合使用?我在 here 找到了以下代码片段,但我宁愿不手动添加这无处不在:
How do I use Unix timestamps with the Doctrine Timestampable behavior? I found the following code snippet here, but I'd rather not manually add this everywhere:
$this->actAs('Timestampable', array(
'created' => array('name' => 'created_at',
'type' => 'integer',
'format' => 'U',
'disabled' => false,
'options' => array()),
'updated' => array('name' => 'updated_at',
'type' => 'integer',
'format' => 'U',
'disabled' => false,
'options' => array())));
推荐答案
这个问题可能比我最初想象的更容易得到答案,实际上......
This is a question that might get an answer easier than what I first thought, actually...
让我们从你现在拥有的开始:
Let's begin by what you have now :
- 一个模型类,它扩展了
Doctrine_Record
- 我将把这个类称为
Test
,作为我的示例. - 在此
Test
模型中,您希望使用Timestampable
行为,但使用 UNIX 时间戳,而不是datetime
值 - 而且您不需要在模型中编写大量配置内容就可以做到这一点.
(我可以理解:在某处忘记一行和 DB 中的错误数据的风险较小)
- a model class, that extends
Doctrine_Record
- I will call this class
Test
, for my example(s). - In this
Test
model, you want to use theTimestampable
Behaviour, but with UNIX timestamps, and notdatetime
values - And you want this without having to write lots of configuration stuff in your models.
(I can understand that : less risk to forget one line somewhere and at wrong data in DB)
- 这意味着您了解 Doctrine 的内容
- 我不会谈论基础知识
解决此问题的方法是不使用 Doctrine 附带的默认
Timestampable
行为,而是使用您将定义的另一种行为.
这意味着,在您的模型中,您将在setTableDefinition
方法的底部有这样的内容:A solution to this problem would be to not use the default
Timestampable
behaviour that comes with Doctrine, but another one, that you will define.
Which means, in your model, you will have something like this at the bottom ofsetTableDefinition
method :$this->actAs('MyTimestampable');
(我想这也可以在
setUp
方法中使用,顺便说一句——实际上它可能是真实的地方)(I suppose this could go in the
setUp
method too, btw -- maybe it would be it's real place, actually)我们现在要做的是定义
MyTimestampable
行为,让它做我们想做的事.
由于 Doctrine 的Doctrine_Template_Timestampable
已经很好地完成了这项工作(当然格式除外),我们将继承它;希望这将意味着编写更少的代码;-)What we now have to do is define that
MyTimestampable
behaviour, so it does what we want.
As Doctrine'sDoctrine_Template_Timestampable
already does the job quite well (except for the format, of course), we will inherit from it ; hopefully, it'll mean less code to write ;-)所以,我们这样声明我们的行为类:
So, we declare our behaviour class like this :
class MyTimestampable extends Doctrine_Template_Timestampable { // Here it will come ^^ }
现在,让我们看看
Doctrine_Template_Timestampable
在 Doctrine 的代码源中实际做了什么:Now, let's have a look at what
Doctrine_Template_Timestampable
actually does, in Doctrine's code source :- 一点配置(
created_at
和updated_at
这两个字段) - 下面这行注册了一个监听器:
$this->addListener(new Doctrine_Template_Listener_Timestampable($this->_options));
我们来看看这个的来源;我们注意到这部分:
Let's have a look at the source of this one ; we notice this part :
if ($options['type'] == 'date') { return date($options['format'], time()); } else if ($options['type'] == 'timestamp') { return date($options['format'], time()); } else { return time(); }
这意味着如果
created_at
和updated_at
两个字段的类型不是date
也不是timestamp
,Doctrine_Template_Listener_Timestampable
将自动使用 UNIX 时间戳——多么方便!This means if the type of the two
created_at
andupdated_at
fields is notdate
nortimestamp
,Doctrine_Template_Listener_Timestampable
will automatically use an UNIX timestamp -- how convenient !由于您不想为每个模型中的这些字段定义
type
,我们将修改我们的MyTimestampable
类.
记住,我们说它是扩展Doctrine_Template_Timestampable
,它负责配置行为...As you don't want to define the
type
to use for those fields in every one of your models, we will modify ourMyTimestampable
class.
Remember, we said it was extendingDoctrine_Template_Timestampable
, which was responsible of the configuration of the behaviour...因此,我们使用
date
和timestamp
以外的type
覆盖该配置:So, we override that configuration, using a
type
other thandate
andtimestamp
:class MyTimestampable extends Doctrine_Template_Timestampable { protected $_options = array( 'created' => array('name' => 'created_at', 'alias' => null, 'type' => 'integer', 'disabled' => false, 'expression' => false, 'options' => array('notnull' => true)), 'updated' => array('name' => 'updated_at', 'alias' => null, 'type' => 'integer', 'disabled' => false, 'expression' => false, 'onInsert' => true, 'options' => array('notnull' => true))); }
我们之前说过,我们的模型是
MyTimestampable
,而不是Timestampable
...所以,现在,让我们看看结果 ;-)We said earlier on that our model was acting as
MyTimestampable
, and notTimestampable
... So, now, let's see the result ;-)如果我们为
Test
考虑这个模型类:If we consider this model class for
Test
:class Test extends Doctrine_Record { public function setTableDefinition() { $this->setTableName('test'); $this->hasColumn('id', 'integer', 4, array( 'type' => 'integer', 'length' => 4, 'unsigned' => 0, 'primary' => true, 'autoincrement' => true, )); $this->hasColumn('name', 'string', 32, array( 'type' => 'string', 'length' => 32, 'fixed' => false, 'primary' => false, 'notnull' => true, 'autoincrement' => false, )); $this->hasColumn('value', 'string', 128, array( 'type' => 'string', 'length' => 128, 'fixed' => false, 'primary' => false, 'notnull' => true, 'autoincrement' => false, )); $this->hasColumn('created_at', 'integer', 4, array( 'type' => 'integer', 'length' => 4, 'unsigned' => 0, 'primary' => false, 'notnull' => true, 'autoincrement' => false, )); $this->hasColumn('updated_at', 'integer', 4, array( 'type' => 'integer', 'length' => 4, 'unsigned' => 0, 'primary' => false, 'notnull' => false, 'autoincrement' => false, )); $this->actAs('MyTimestampable'); } }
映射到以下 MySQL 表:
Which maps to the following MySQL table :
CREATE TABLE `test1`.`test` ( `id` int(11) NOT NULL auto_increment, `name` varchar(32) NOT NULL, `value` varchar(128) NOT NULL, `created_at` int(11) NOT NULL, `updated_at` int(11) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
我们可以这样在表中创建两行:
We can create two rows in the table this way :
$test = new Test(); $test->name = 'Test 1'; $test->value = 'My Value 2'; $test->save(); $test = new Test(); $test->name = 'Test 2'; $test->value = 'My Value 2'; $test->save();
如果我们检查数据库中的值,我们会得到这样的结果:
If we check the values in the DB, we'll get something like this :
mysql> select * from test; +----+--------+----------------+------------+------------+ | id | name | value | created_at | updated_at | +----+--------+----------------+------------+------------+ | 1 | Test 1 | My Value 1 | 1248805507 | 1248805507 | | 2 | Test 2 | My Value 2 | 1248805583 | 1248805583 | +----+--------+----------------+------------+------------+ 2 rows in set (0.00 sec)
所以,我们似乎可以创建行了 ;-)
So, we are OK for the creation of rows, it seems ;-)
现在,让我们获取并更新第二行:
And now, let's fetch and update the second row :
$test = Doctrine::getTable('Test')->find(2); $test->value = 'My New Value 2'; $test->save();
然后,回到数据库,我们现在得到了这个:
And, back to the DB, we now get this :
mysql> select * from test; +----+--------+----------------+------------+------------+ | id | name | value | created_at | updated_at | +----+--------+----------------+------------+------------+ | 1 | Test 1 | My Value 1 | 1248805507 | 1248805507 | | 2 | Test 2 | My New Value 2 | 1248805583 | 1248805821 | +----+--------+----------------+------------+------------+ 2 rows in set (0.00 sec)
updated_at
字段已更新,created_at
字段未更改;这似乎也不错;-)The
updated_at
field has been updated, and thecreated_at
field has not changed ; which seems OK too ;-)所以,为了简短起见,请放在几个要点中,然后总结一下:
So, to make things short, fit in a couple of bullet points, and summarize quite a bit :
- 我们的模型类充当我们自己的
MyTimestampable
,而不是默认的Timestampable
- 我们的行为扩展了 Doctrine 的行为
- 并且只覆盖它的配置
- 所以我们可以随心所欲地使用它,每个模型中只有一行代码.
我会让你做一些更密集的测试,但我希望这会有所帮助!
玩得开心:-)这篇关于在 Doctrine Timestampable 中使用 Unix 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
- I will call this class
- 我将把这个类称为