Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Observer for before_get or similar?
  • Hey... I'd like to make sure a where clause is always passed when data is requested from some of my models, i guessed an observer would be perfect for this, so that every time we request records, it will always add this additional where clause if it doesn't already exist. Is it possible to have a before_get, or is that already included? Matt
  • Afaik that doesn't exist. Please add a feature request for this at http://github.com/fuel/orm/issues.
  • It's not possible because of how observers work: they act upon individual objects but before you got those there's nothing to act upon and thus nothing for the observer to do. If you want something like this adding an extension to the find() method or query() method is probably the way to go.
  • I've put a dirty hack in to solve my problem for the moment, i will clean this up for production use, but thought i'd share this to see if it's something that could be achieved elsewhere, in the function get() in query.php i've put the following code in: For forcing account_id in select's i've put the following in the function get() in query.php:
    // Check it has account_id
    $table_name = call_user_func($this->model.'::table');
    $locked_tables =  array('table_a', 'table_b', 'table_c'); // im moving this to a config
    if(in_array($table_name, $locked_tables)) $this->where('account_id', \Session::get('account_id'));
    

    And i've created an observer inside the necessary models for before_insert to ensure that the same tables are forced to pass up an account_id when they are inserted into the database, clearly this is more hacky than i'd like but it would be a nice feature to try and find a place for?

Howdy, Stranger!

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

In this Discussion