在Doctrine Timestampable中使用Unix时间戳

6 php doctrine epoch unix-timestamp

如何将Unix时间戳与Doctrine Timestampable行为一起使用?我在这里找到了以下代码片段,但我不想在任何地方手动添加它:

$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())));
Run Code Online (Sandbox Code Playgroud)

Pas*_*TIN 33

这个问题可能比我最初的想法更容易得到答案,实际上......

让我们从你现在拥有的东西开始吧:

  • 扩展的模型类 Doctrine_Record
    • Test对于我的例子,我会打电话给这个班级.
    • 在此Test模型中,您希望使用Timestampable行为,但使用UNIX时间戳,而不是datetime
    • 而且你想要这个,而不必在你的模型中写很多配置.
      (我可以理解:在某处忘记一行以及DB中的错误数据的风险较小)
  • 已配置的项目和所有内容
    • 这意味着你了解Doctrine的东西
    • 而且我不会谈论基础知识

这个问题的解决方案是不使用TimestampableDoctrine附带的默认行为,而是使用另一个您将定义的行为.
这意味着,在您的模型中,您将在setTableDefinition方法的底部有类似的内容:

$this->actAs('MyTimestampable');
Run Code Online (Sandbox Code Playgroud)

(我想这也可以用在setUp方法中,顺便说一句 - 也许它会是真实的地方,实际上)


我们现在要做的是定义这种MyTimestampable行为,这样就可以实现我们想要的.
由于Doctrine Doctrine_Template_Timestampable已经完成了这项工作(当然除了格式),我们将继承它; 希望,它意味着更少的代码写;-)

所以,我们声明我们的行为类是这样的:

class MyTimestampable extends Doctrine_Template_Timestampable
{
    // Here it will come ^^
}
Run Code Online (Sandbox Code Playgroud)

现在,让我们看看Doctrine_Template_TimestampableDoctrine的代码源中实际做了什么:

  • 一点配置(两个created_atupdated_at字段)
  • 以下行注册了一个监听器:


$this->addListener(new Doctrine_Template_Listener_Timestampable($this->_options));
Run Code Online (Sandbox Code Playgroud)

我们来看看这个的来源; 我们注意到这一部分:

if ($options['type'] == 'date') {
    return date($options['format'], time());
} else if ($options['type'] == 'timestamp') {
    return date($options['format'], time());
} else {
    return time();
}
Run Code Online (Sandbox Code Playgroud)

这意味着如果两个created_atupdated_at字段的类型不是date也没有timestamp,Doctrine_Template_Listener_Timestampable将自动使用UNIX时间戳 - 多么方便!


由于您不想type在每个模型中定义要用于这些字段,我们将修改我们的MyTimestampable类.
记住,我们说它正在扩展Doctrine_Template_Timestampable,它负责行为的配置......

因此,我们使用typedate和之外的其他方式覆盖该配置timestamp:

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)));
}
Run Code Online (Sandbox Code Playgroud)

我们之前说过,我们的模型是作为MyTimestampable,而不是Timestampable......所以,现在,让我们看看结果;-)

如果我们考虑这个模型类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');
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个映射到以下MySQL表:

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
Run Code Online (Sandbox Code Playgroud)

我们可以这样在表中创建两行:

$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();
Run Code Online (Sandbox Code Playgroud)

如果我们检查数据库中的值,我们会得到这样的结果:

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)
Run Code Online (Sandbox Code Playgroud)

所以,我们可以创建行,似乎;-)


现在,让我们获取并更新第二行:

$test = Doctrine::getTable('Test')->find(2);
$test->value = 'My New Value 2';
$test->save();
Run Code Online (Sandbox Code Playgroud)

而且,回到数据库,我们现在得到这个:

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)
Run Code Online (Sandbox Code Playgroud)

updated_at字段已更新,created_at字段未更改; 这似乎也好;-)


所以,为了简短起见,要适应几个要点,总结一下:

  • 我们的模型类充当我们自己的模型类MyTimestampable,而不是默认模型Timestampable
  • 我们的行为延伸了Doctrine的行为
  • 并且只覆盖它的配置
    • 因此我们可以根据需要使用它,每个模型中只有一行代码.


我会让你做一些更密集的测试,但我希望这有帮助!
玩得开心:-)

  • 这是一个很好的答案,应该是+100. (3认同)