在CakePHP 2.0的夹具上添加当前时间戳

时间:2012-04-14 17:39:12

标签: unit-testing cakephp timestamp cakephp-2.0 fixtures

我正在尝试测试模型,我需要在'created'字段中插入当前时间戳。

我正在使用Fixture,但使用'NOW()'似乎不起作用。它写的是00:00:00。

这是我的夹具记录:

public $records = array(
        array(
            'id' => 1,
            'body' => 'esto es una prueba',
            'idUser' => 2,
            'created' => 'NOW()',
            'ip' =>'1.1.1.1',
         )
)

对此有何想法? 感谢。

2 个答案:

答案 0 :(得分:2)

由于$records是一个类属性,因此不能在定义中使用函数。 如果你想使用函数(比如你的date()或类似函数),你可以在init函数中这样做:

public function init() {
  $this->records = array(
    // ...
    'created' => date('Y-m-d H:i:s'),
    // ...
  );

  // dont forget to call parent::init() if you're overwriting init().
  parent::init();
}

答案 1 :(得分:-1)

public $records = array(
        array(
            'id' => 1,
            'body' => 'esto es una prueba',
            'idUser' => 2,
            'created' => date('Y-m-d h:i:s'), // in this case `created` must have to be a DATETIME type
            'ip' =>'1.1.1.1',
         )
);

public $records = array(
            array(
                'id' => 1,
                'body' => 'esto es una prueba',
                'idUser' => 2,
                'created' => time(), // in this case `created` must have to be a TIMESTAMP type
                'ip' =>'1.1.1.1',
             )
    );