0

I am trying to test a Model and i need to insert into the 'created' field the current time-stamp.

I am using a Fixture for it but using 'NOW()' doesn't seem to work. It writes 0000-00-00 00:00:00.

This is my fixture record:

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

Any idea about this? Thanks.

Alvaro
  • 40,778
  • 30
  • 164
  • 336
  • possible duplicate of [Submitting current timestamp in CakePHP](http://stackoverflow.com/questions/3823169/submitting-current-timestamp-in-cakephp) – Hardik Sondagar Apr 07 '14 at 09:07

2 Answers2

2

As $recordsis a class property, you cannot use functions inside the definition. If you want to use functions (like in your case date()or similar) you've to do it inside the init function like this:

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();
}
rookian
  • 1,045
  • 14
  • 38
-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',
         )
);

OR

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',
             )
    );
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • None of them works. Test can not run. I have already tried it. Also, both of them shows semantic errors before running them. – Alvaro Apr 15 '12 at 16:59