Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Passing config params to an Orm Observer
  • Hi,
    I'm trying to use an observer to intercept fields before save and after retrieval, so that I can encrypt some values.

    I want to be able to exclude certain fields defined from within the model itself, but I'm unsure how to approach this. I've tried the following:

    Within my model:

    protected static $_observers = array(
    'Encryption' => array(
    // set field true to exclude from validation
    'exclude' => array(
    'size' => true
    )
    ),
    )


    class Observer_Encryption extends Orm\Observer
    {
    public static $exclude = array();

    public function __construct($class)
    {
    $props = $class::observers(get_class($this));
    $this->_props = $props;
    $this->_exclude = isset($props['exclude']) ? $props['exclude'] : static::$exclude;
    }

    // called by the save() method before anything is done
    public function after_load(Orm\Model $model)
    {
    \Log::info('[props val: '.print_r( $this->_props, true ));

    foreach ($model as $key => $value) {
    if ($key !='id' && $key != 'updated_at' && $key != 'created_at' && $key != 'size'){
    $model->$key = Crypt::decode($value);
    }
    }

    }
    }

    When I log _props or _exclude I've no values set, should I be able to pass in params in this way?
    Thanks in advance!
  • Maybe this helps.

    This is one of our observers, that uses properties: https://bin.fuelphp.com/snippet/view/OX

    The observer is defined in the model like so: https://bin.fuelphp.com/snippet/view/OY
  • Thanks for this, I have the code together but I still can't seem to get the props to come through, perhaps its due to name spacing or similar? I have the observer in classes/observer/encryption defined as "Observer_Encryption" 

    I can't work out why the param isn't being passed to the observer :(


  • class names in strings must always be fully qualified, so 

    'Encryption' => array(

    is not correct, you need to include the namespace in there, like you do for the other two.
  • Interestingly though, it is loading that observer correctly. I'll try the full path and see if it works thanks
  • HarroHarro
    Accepted Answer
    That might work if it's in the current namespace. 

    But the props are accessed like so:

    $props = $class::observers(get_class($this));

    which fails since get_class() returns a fully qualified classname, including namespace, but the props are stored without the namespace.
  • Ah, that makes sense. I'm just struggling with determining the actual namespace for the encryption.php file I've placed in /classes/observer/

    I really need to brush up on how the framework uses the file paths to establish the namespace.

    I tried to add:

    namespace Model; 

    class Observer_Encryption extends Orm\Observer

    As shown in your example, and then add the observer as 

    Model\Observer_Encryption

    to the model

    But that triggers throw new \UnexpectedValueException($observer)

  • HarroHarro
    Accepted Answer
    If the file is /classes/observer/encryption.php, and the class is called "Observer_Encryption", then it doesn't have a namespace (it is in the global namespace).

    This explains exactly how it works: https://fuelphp.com/docs/general/namespacing.html

    This in turn means the correct definition should be:

    'Observer_Encryption' => array(
  • Thanks, that is how it's currently set. But that isn't allowing params to be passed, so I need to set a namespace presumably so I can have a fully qualified namespace?

    Thanks
  • Ok, i've moved it to model/observer/encryption

    set the namespace to 
    namespace Model;

    class Observer_Encryption extends \Orm\Observer

    Defined it in the model as 
    'Model\Observer_Encryption'

    Bingo, it's working!

    Thanks a lot!


Howdy, Stranger!

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

In this Discussion