Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Observers
  • Hello,

    I am trying to understand and use the observers in my models.

    In each of my models, I have the following code :

        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,
            ),
        );

    I tought that this would automatically update the field 'updated_at' in my tables, executing the function 'before_save', but it does not.
    I started my project in fuelphp 1.4 and upgraded at each release, could there be a problem there ?
    I undestood that now the fields are added in the properties which i corrected, but it does not improve the behaviour I have.

    What did I miss or misunderstood to have this working ?

    Thank you for your help,
    Regards,
    Herve.
  • That is indeed the idea behind it, and it works fine here.

    Defined like this, your model must define columns called 'created_at' and 'updated_at', which must be created as "unsigned int" to store a unix timestamp.

    Note that UpdatedAt is only triggered on update, not on insert, so the first save should only update the created_at column, subsequent saves only the updated_at column. Also Note that if you save an ORM object without changing anything, it will not run an UPDATE query, and the column will not be updated.
  • So that's what I have.
    I have just made some tests to verify, and created_at and updated_at are not updated except when I create a user with simple_auth then the created_at is set. In all other cases there is no update in all the tables I have.
    Just to make sure I also replaced this
                'created_at' => array('constraint' => 11, 'type' => 'int'),
                'updated_at' => array('constraint' => 11, 'type' => 'int'),
    by this
                'created_at' => array('constraint' => 11, 'type' => 'int', 'unsigned' => true),
                'updated_at' => array('constraint' => 11, 'type' => 'int', 'unsigned' => true),
    I even broke my database and recreated it with the migration process, but that does not change anything.
    I don't kow where else to search...
    Any idea ?

  • I am clueless. This is what our base model defines in it's _init method:

    static::$_observers['Orm\\Observer_CreatedAt'] = array('events' => array('before_insert'),'property' => 'created_at', 'mysql_timestamp' => false);
    static::$_observers['Orm\\Observer_UpdatedAt'] = array('events' => array('before_update'), 'property' => 'updated_at', 'mysql_timestamp' => false);

    And that works as advertised.
  • there is another deleted_at , do you know how to set its observe?
  • I don't understand the question. If you delete a record, it's gone, so why add a deleted_at column?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion