<bdo id='rhO8O'></bdo><ul id='rhO8O'></ul>

      <i id='rhO8O'><tr id='rhO8O'><dt id='rhO8O'><q id='rhO8O'><span id='rhO8O'><b id='rhO8O'><form id='rhO8O'><ins id='rhO8O'></ins><ul id='rhO8O'></ul><sub id='rhO8O'></sub></form><legend id='rhO8O'></legend><bdo id='rhO8O'><pre id='rhO8O'><center id='rhO8O'></center></pre></bdo></b><th id='rhO8O'></th></span></q></dt></tr></i><div id='rhO8O'><tfoot id='rhO8O'></tfoot><dl id='rhO8O'><fieldset id='rhO8O'></fieldset></dl></div>
      <legend id='rhO8O'><style id='rhO8O'><dir id='rhO8O'><q id='rhO8O'></q></dir></style></legend>
      1. <small id='rhO8O'></small><noframes id='rhO8O'>

        <tfoot id='rhO8O'></tfoot>
      2. 在 Doctrine Timestampable 中使用 Unix 时间戳

        时间:2024-08-09

      3. <small id='BOtGE'></small><noframes id='BOtGE'>

      4. <i id='BOtGE'><tr id='BOtGE'><dt id='BOtGE'><q id='BOtGE'><span id='BOtGE'><b id='BOtGE'><form id='BOtGE'><ins id='BOtGE'></ins><ul id='BOtGE'></ul><sub id='BOtGE'></sub></form><legend id='BOtGE'></legend><bdo id='BOtGE'><pre id='BOtGE'><center id='BOtGE'></center></pre></bdo></b><th id='BOtGE'></th></span></q></dt></tr></i><div id='BOtGE'><tfoot id='BOtGE'></tfoot><dl id='BOtGE'><fieldset id='BOtGE'></fieldset></dl></div>
          <tfoot id='BOtGE'></tfoot>
            <bdo id='BOtGE'></bdo><ul id='BOtGE'></ul>

                  <legend id='BOtGE'><style id='BOtGE'><dir id='BOtGE'><q id='BOtGE'></q></dir></style></legend>
                    <tbody id='BOtGE'></tbody>
                  本文介绍了在 Doctrine Timestampable 中使用 Unix 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何将 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 the Timestampable Behaviour, but with UNIX timestamps, and not datetime 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 of setTableDefinition 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's Doctrine_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_atupdated_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_atupdated_at 两个字段的类型不是 date 也不是 timestampDoctrine_Template_Listener_Timestampable 将自动使用 UNIX 时间戳——多么方便!

                      This means if the type of the two created_at and updated_at fields is not date nor timestamp, 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 our MyTimestampable class.
                      Remember, we said it was extending Doctrine_Template_Timestampable, which was responsible of the configuration of the behaviour...

                      因此,我们使用 datetimestamp 以外的 type 覆盖该配置:

                      So, we override that configuration, using a type other than date and 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)));
                      }
                      

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

                      We said earlier on that our model was acting as MyTimestampable, and not Timestampable... 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 the created_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 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Symfony 2 嵌入式表单集合多对多 下一篇:如何在 Doctrine 查询中指定空值作为过滤器?

                  相关文章

                  1. <legend id='B7a48'><style id='B7a48'><dir id='B7a48'><q id='B7a48'></q></dir></style></legend>
                      <bdo id='B7a48'></bdo><ul id='B7a48'></ul>

                    <small id='B7a48'></small><noframes id='B7a48'>

                  2. <tfoot id='B7a48'></tfoot>
                    <i id='B7a48'><tr id='B7a48'><dt id='B7a48'><q id='B7a48'><span id='B7a48'><b id='B7a48'><form id='B7a48'><ins id='B7a48'></ins><ul id='B7a48'></ul><sub id='B7a48'></sub></form><legend id='B7a48'></legend><bdo id='B7a48'><pre id='B7a48'><center id='B7a48'></center></pre></bdo></b><th id='B7a48'></th></span></q></dt></tr></i><div id='B7a48'><tfoot id='B7a48'></tfoot><dl id='B7a48'><fieldset id='B7a48'></fieldset></dl></div>