fuel / orm

Fuel PHP Framework - Fuel v1.x ORM
http://fuelphp.com/docs/packages/orm/intro.html
152 stars 96 forks source link

timestamps resetting to 0 #207

Closed notrab closed 11 years ago

notrab commented 11 years ago

Migrations use int for the created_at/updated_at but the ORM is using the mysql_timestamps

protected static $_observers = array(
'Orm\Observer_CreatedAt' => array(
    'events' => array('before_insert'),
    'mysql_timestamp' => false,
),
'Orm\Observer_UpdatedAt' => array(
    'events' => array('before_save'),
    'mysql_timestamp' => false,
),
);
notrab commented 11 years ago

After looking on the forums someone else has reported this issue here: http://fuelphp.com/forums/discussion/11801/generate-uses-int-in-migration-mysql_timestamp-in-model-observer#Item_1

WanWizard commented 11 years ago

Can't reproduce it.

I've setup new 1.5.1. install. I created this table:

CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `created_at` int(11) DEFAULT NULL,
  `updated_at` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

this model:

<?php
class Model_Test extends \Orm\Model {

    protected static $_properties = array(
        'id',
        'name',
        'created_at',
        'updated_at',
    );

    /*
     * @var string  name of the table
     */
    protected static $_table_name = 'test';

    protected static $_observers = array(
        'Orm\Observer_CreatedAt' => array(
            'events' => array('before_insert'),
            'mysql_timestamp' => false,
        ),
        'Orm\Observer_UpdatedAt' => array(
            'events' => array('before_update'),
            'mysql_timestamp' => false,
        ),
    );

}

And this test controller:

<?php
class Controller_Test extends Controller
{
    public function action_create()
    {
        \Package::load('orm');

        $test = \Model_Test::forge();
        $test->name = 'test';
        $test->save();
    }

    public function action_update()
    {
        \Package::load('orm');

        $test = \Model_Test::find_by_name('test');
        $test->name = 'test update';
        $test->save();
    }
}

The debug shows that after action_create(), created_at is assigned a value, and after action_update() the updated_at is assigned a value too. So it looks like it works as advertised.

What I do see is that your snippet does an update 'before_save'. If that is what is generated, then that is incorrect, it should be 'before_update'.

pierot commented 11 years ago

I have the samen problem. Since 1.5 the updated_at and created_at fields in the database are all set to 0. If I try to debug the Observer_CreatedAt, the value is generated but somehow it is not saved properly.

Thanks for looking into this.


I looked into the created_at and updated_at some more and it seems (I might be wrong) that in 1.5+ the created_at and updated_at properties are not included in the generated models (from oil) unless explicitly given with an option 'crud'. After adding the properties, all works well.

WanWizard commented 11 years ago

So it's a oil issue, not an ORM issue. If it still exists (i.e. not already fixed in 1.6/develop), please create an issue at http://github.com/fuel/oil/issues.