Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Trouble with Observers
  • I've used scaffolding to set up a foo table with created_at and updated_at columns (as well as the necessary V, M & C). I then added the Created At and Updated at observers to the model, but I can't seem to get them to work. When I add a new record, both fields are set to 0000-00-00 00:00:00. When I edit a record for the first time, created_at is set to NULL. If I subsequently edit the same record without changing any of the fields, I get two notices after submitting:
    1. ErrorException [ Notice ]: Undefined variable: foo
    2. ErrorException [ Notice ]: Trying to get property of non-object
    - both on line 5:
    <?php echo Html::anchor('foos/view/'.$foo->id, 'View'); ?> |
    

    The Code:
    class Model_Foo extends Orm\Model &#123;
    
     protected static $_properties = array(
      'id',
      'created_at',
      'updated_at',
      'name'
     );
     protected static $_observers = array(
      'Orm\\Observer_CreatedAt' => array('before_insert'),
      'Orm\\Observer_UpdatedAt' => array('before_save')
     );
    }
    
    If I comment out the $_observers lines, everything works fine - no errors, but created_at and updated_at remain NULL (obviously). I have tried with a datatype of datetime and timestamp on the created and updated at fields - doesn't seem to make a difference. Using Fuel 1.0 RC2.
  • I'm going to go out on a limb here, but it seems that the results in the ternary operator are switched around (either that, or the static property $mysql_timestamp is confusingly named). When $mysql_timestamp is true, before_insert() sets the property to \Date::time()->format('mysql')
    When it's false, the property is set to \Date::time()->get_timestamp() Original:
    class Observer_CreatedAt extends Observer {
    
     public static $mysql_timestamp = false;
     public static $property = 'created_at';
    
     public function before_insert(Model $obj)
     {
      $obj->{static::$property} = static::$mysql_timestamp ? \Date::time()->format('mysql') : \Date::time()->get_timestamp();
     }
    }
    
    Proposed change:
    class Observer_CreatedAt extends Observer {
    
     public static $mysql_timestamp = false;
     public static $property = 'created_at';
    
     public function before_insert(Model $obj)
     {
      $obj->{static::$property} = static::$mysql_timestamp ? \Date::time()->get_timestamp() : \Date::time()->format('mysql');
     }
    }
    

    The same situation seems to exist in UpdatedAt. I've made the recommended change in both classes, and the observers seem to me to be working correctly, now. I'll be happy to report this in the issue tracker if someone can confirm that I'm not making a dumb newb mistake here.
  • Edit: So I think maybe I was wrong above - I simply misinterpreted the meaning of the variable name.

Howdy, Stranger!

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

In this Discussion