fuel->config() persistance

edited May 2015 in Modules
Hi

When saving new pages, a navigation item is created at the same time with a group_id that uses the contents of the 'auto_page_navigation_group_id' setting.

I'm using the $limit_to_user_field functionality and need to specify what group_id should be used for the navigation item when saving the page. I've extended Fuel_pages_model and in the on_before_save() function I've set
$this->fuel->set_config('auto_page_navigation_group_id',10);
Where '10' is the group_id I want to use. Testing later in the same function using
$this->fuel->config('auto_page_navigation_group_id')
returns '10' - which is what I want.

In _save_page_vars() function of ./fuel/modules/fuel/controller/pages.php - where the navigation item is created, the same check returns '1'.

Does that mean that changing the settings using set_config() does not persist outside the calling function?

This does seem to be a bit messy as a solution. Is there a different approach I'm missing?

Cheers

Comments

  • edited 3:05AM
    No, it should persist it to the CI config object ($this->config->item('fuel') should yield all the config variables. It doesn't however, persist the data to storage so subsequent calls would still show 1.

    Is there any conditional logic in the on_before_save where in some instances the "auto_page_navigation_group_id" config value would not be set to 10?
  • edited 3:05AM
    Hmm. Something's not right.

    There is logic in the on_before_save() but I've got a log_message() on the last line before the return which outputs '10'.

    I also have a log_message() on the first line of pages::_save_page_vars() which records $this->config->item('fuel') and shows auto_page_navigation_group_id = 1
  • edited 3:05AM
    This is weird.

    In pages::create(), I've edited:
    // run before_save hook $this->_run_hook('before_save', $posted); log_message('DEBUG','Current Config:: '.print_r($this->config->item('fuel'),true));

    I'm setting 'auto_page_navigation_group_id' in the model's on_before_save() - which is called by the _run_hook(). If I look in the logs however the config array is printed before my log_message() in on_before_save(). Surely it should be after?.. It certainly explains why the "auto_page_navigation_group_id" is not being picked up properly
  • edited 3:05AM
    The $this->_run_hook() call is for module hooks and not model hooks (more on the difference here):
    http://docs.getfuelcms.com/modules/hooks
  • edited 3:05AM
    Thanks. I think I understand the difference but I'm not sure how relevent that is. I'm using all Fuel native code. I've not changed anything just created a on_before_save() function in my over-ridden Fuel_pages_model (My_fuel_pages_model).

    Are you saying that since I've over-ridden Fuel_pages_model, I now have a completely seperate model and the native Pages controller can no longer process _run_hook()?

    That does not seem to be the case, my on_before_save() does seem to get processed by the native Pages controller, the problem seems to be that the controller is ignoring my changes to the CI Config and, weirdly, my log_message seems to be processed before the _run_hook callback has finished.

    Perhaps I'm doing this all wrong?

    If I'm using the $limit_to_user_field functionality in an over-riodden Fuel_pages_model, how can I force the Pages controller to use a group_id of my (dynamic) choice rather than what's hard-coded into the CI config object?
  • edited 3:05AM
    This is what I've done to try and replicate your issue and it seems to work:

    1. Added the following to the fuel/application/config/MY_fuel_modules.php file:
    $config['module_overwrites']['pages']['model_name'] = 'my_pages_model'; $config['module_overwrites']['pages']['model_location'] = 'app';

    2. Create a fuel/application/models/my_pages_model.php file with the following in it:
    require_once(FUEL_PATH.'models/fuel_pages_model.php'); class My_pages_model extends Fuel_pages_model { public $limit_to_user_field = TRUE; public function on_before_save($values) { $values = parent::on_before_save($values); $this->fuel->set_config('auto_page_navigation_group_id', 10); return $values; } }
    Creating a new page with the navigation item is getting set to 10 when I do it this way. Is this along the lines of what you are doing and am I missing a step?
  • edited 3:05AM
    Yes, that's pretty much what I have too (of course, I have extra code to calculate the actual group_id that I want to use).

    I also have code that saves the original auto_page_navigation_group_id to my_page_model::$orig_group_id and then in the on_after_save(), I put it back. It seems that if I skip the on_after_save() phase (and therefore duplicate your example) - it works!
    require_once(FUEL_PATH.'models/fuel_pages_model.php'); class My_pages_model extends Fuel_pages_model { public $limit_to_user_field = TRUE; public $orig_group_id = 1; public function on_before_save($values) { $values = parent::on_before_save($values); $this->orig_group_id = $this->fuel->config('auto_page_navigation_group_id'); $this->fuel->set_config('auto_page_navigation_group_id', 10); return $values; } public function on_after_save($values) { $values = parent::on_after_save($values); $this->fuel->set_config('auto_page_navigation_group_id', $this->orig_group_id); return $values; } }
Sign In or Register to comment.