Note the upgraded forum! If you are experiencing issues logging in, you may need to reset your password which should send an email. If the email doesn't arrive, be sure to check your spam folder just in case.

[Solved] Issues accessing method in an advanced module from helper function.

edited March 2018 in Modules
I have created an advanced module whose main purpose is to call a 3rd party API and persist a bunch of returned values into the database. When users view them, I'd like to every so often refresh the values on-the-fly.

I added a function to the advanced module's helper functions file. It is added to Twig's "allowed_functions" list and so people can type `{show_ads()}` on a page and it should display the ads list. The function looks roughly as follows:

if (!function_exists('show_ads')) { function show_ads() { $CI = &get_instance(); // Refresh if needed $CI->fuel->my_advanced_module->refresh_cache(); // Load ads for display ... } }

To ensure that the 3rd party is not called every time someone views a page, I'd like to keep track of the last time a refresh occurred in a FUEL Site Variable.

class Fuel_my_advanced_module extends Fuel_advanced_module { // ... public function __construct($params = array()) { parent:GDN__construct(); $this->initialize($params); $this->fuel->load_model('fuel_sitevariables'); } function refresh_cache($force = FALSE) { // Call 3rd party, refresh cache.. // Update site variable $existing = $this->fuel_sitevariables_model->find_one(array( 'name' => 'last_refresh', 'active' => 'yes' )); if (empty($existing)) { $existing = $this->fuel->sitevars->create(); $existing->name = 'last_refresh'; } $existing->value = datetime_now(); $existing->save(); } }

`Fuel_sitevariables_model` extends `MY_Model` so it should have access to `find_one`. However upon invoking `$this->fuel_sitevariables_model->find_one` I end up in https://github.com/daylightstudio/FUEL-CMS/blob/master/fuel/modules/fuel/libraries/Fuel_modules.php#L1136 The `find` method does things differently to `find_one` and I do not get the desired functionality.

What am I doing wrong? I haven't put code in a class that extends `Fuel_advanced_module` before so this might be something trivial.

Thank you for the help.

Comments

  • edited 8:11AM
    Been battling this since yesterday evening. Just started a thread here and then solved the issue!

    I basically just replaced

    $this->fuel_sitevariables_model->find_one

    with

    $this->CI->fuel_sitevariables_model->find_one

    Interesting how that works. Usually when I want to access a model, I can load it using `$this->load->model('my_model')` and then just use it as `$this->my_model->find_one`. Are advanced modules way different in that regard?
  • edited 8:11AM
    This is actually a fundamental part of how CodeIgniter works. "$this" actually refers to the Controller object which is used to load resources etc. If you want to reference that object from within a different context, you must do something like the following:
    $CI =& get_instance();
    OR the FUEL specific function that returns that reference:
    CI()
    FUEL's base model and libraries provide this reference for you through the CI property (which you discovered). Additionally, the fuel/application/views/_variables/global.php file which is used to inject global variables for view files has a $CI variable.
Sign In or Register to comment.