Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
before_insert event in Observer_Typing
  • Hi!

    There is no `before_insert` event in `\Orm\Observer_Typing`.
    Is there any reason for it?
    It could be useful here:

    class Model_Property extends \Orm\Model 
    {
    protected static $_properties = array(
    'name',
    'custom' => array(
    'data_type' => 'serialize',
    ),
    );

    protected static $_observers = array(
    'Orm\\Observer_Self',
    'Orm\\Observer_Typing'
    );

    public function _event_before_insert()
    {
    $this->custom = \Config::get('defaults.property.custom', array());
    }
    }


    What is happening now:

    $property = Model_Property::forge(array('name' => 'Name'))
    $property->save();

    // launch `before_save` observers:
    // Observer_Self::_event_before_save();
    // $property->custom is still empty here
    // Observer_Typing::_event_before_save();

    // launch `before_insert` observers:
    // Observer_Self::_event_before_insert();
    // $property->custom = array()

    // Catching Database_Exception here because `custom` property is unserialized yet.


    Sure, I can use it that way, but why?

    public function _event_before_save()
    {
    if ($this->is_new())
    {
    $this->custom = \Config::get('defaults.property.custom', array());
    }
    }
  • The Typing observer is about value type conversions between database format and internal model format, that is not related to weather a write is an insert or an update.

    If you mean observers in general, that event already exists, see https://fuelphp.com/docs/packages/orm/observers/creating.html#/event_names
  • Type conversation is what I want to do, but in this case I can't.

    I need to update some model property in `_event_before_insert()` method before save new model. For some reason I can't do it in Controller. 
    And then serialize it by Typing observer. 
    But there are only following events in `\Orm\Observer_Typing` class:

    class Observer_Typing
    {
    public static $events = array(
    'before_save' => 'before',
    'after_save' => 'after',
    'after_load' => 'after',
    );
    }
  • And I'm interested if there is any special reason why `Typing_Observer` doesn't have `before_insert` event?
  • Harro VertonHarro Verton
    Accepted Answer
    The typing observer converts field types, and that is bi-directional. And it is not for initializing values, setting values from other locations, etc.

    It doesn't need a before_insert or a before_update because it needs to do the same in both cases. 

    You want to mis-use it for other purposes, which are not supported.

Howdy, Stranger!

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

In this Discussion