CodeIgniter Forums
[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: [solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... (/showthread.php?tid=8448)

Pages: 1 2


[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - El Forum - 05-18-2008

[eluser]differ[/eluser]
Hi,

I encountered problem in loading model/library from hook. Hook is specified on pre_controller. Is there any way to load model?

Tricks connected with:
Code:
$ci =& get_instance();
$ci->load->model('model');

... does not work. When trying to load:

Code:
$this->load->model('model_name');

PHP throws error:

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: Welcome::$model_name
Filename: libraries/Loader.php
Line Number: 980

I forgot about fatal error:

Quote:Fatal error: Call to a member function _assign_libraries() on a non-object in /system/libraries/Loader.php on line 980

My example hook controller:

Code:
class Example extends Controller
{
    public function __construct()
    {
        parent::Controller();
    }

    public function hookFunc()
    {
        $this->load->model('model_name');
    }
}

... and hooks config:

Code:
$hook['pre_controller'] = array(
    'class'       =>    'Example',
    'function'    =>    'hookFunc',
    'filename'    =>    'Example.php',
    'filepath'    =>    'hooks'
    );

Where is the problem? Any advice will be appreciated.


[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - El Forum - 05-19-2008

[eluser]differ[/eluser]
Is it possible to do that? Maybe someone from CI Team can say something about problem?

It is very important.


[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - El Forum - 05-19-2008

[eluser]zdknudsen[/eluser]
I don't think you should extend your hooks from the Controller class. This might also be what is causing the fatal error about _assign_libraries().

Try just boiling it down to
Code:
class Example
{
    public function hookFunc()
    {
        $ci = &get;_instance();
        $ci->load->model('model_name');
    }
}

Note: Please notice that there is a misplaced ; in the get_instance function, that you need to remove if you copy+paste it. The forums wont let me remove it.


[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - El Forum - 05-19-2008

[eluser]wiredesignz[/eluser]
The CI core (controller) does not exist yet so using get_instance() in a pre_controller hook is not possible.

@Zach, you can use this layout in your code block.
Code:
$ci =& get_instance();



[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - El Forum - 05-19-2008

[eluser]zdknudsen[/eluser]
He is quite right. You might want to try the post_controller_constructor hook instead.

@wired: Neat, it have been annoying me numerous times. Smile


[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - El Forum - 05-20-2008

[eluser]differ[/eluser]
Nope post_controller is not right because I must load some code before controller will execute its __construct() function.
If it is not possible I must "hack" hook class and insert clear php code without diving them into models, libraries etc...

Thanks for help.


[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - El Forum - 05-20-2008

[eluser]mironcho[/eluser]
What about autoloading some library or model (config/autoload.php)? Prior to CI version 1.6, I've used this approach - I had simple DummySetNames library (libraries/dummysetnames.php) with the only task to set db connection charset:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class DummySetNames
{
    function DummySetNames()
    {
        $this->ci = & get_instance();
        $this->ci->db->query("SET NAMES ?", $this->ci->config->item('mysql_charset'));
    }
}

?>



[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - El Forum - 05-20-2008

[eluser]differ[/eluser]
Hi mironcho,

Okay but tell me when autoload is executed, after initializing hooks or before?
I need to find some workaround. It is an acl system, so I need to check user privileges before controller execute its constructor.
In hookFunc I have declared code which should verify whether user is valid and has privileges to execute specified controller or not. So I need to load libraries and models in hookFunc.

Thanks for answers.


[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - El Forum - 05-20-2008

[eluser]mironcho[/eluser]
Autoloading is done in main controller's constructor (parent::Controller()). It's executed after pre_controller hook, but before yours constructor code (if invoking parrent::Controller() is the first thing in your constructor).
Take a look at system/codeigniter/CodeIgniter.php for complete application flow...


[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object... - El Forum - 05-20-2008

[eluser]differ[/eluser]
I "hacked" system/libraries/Controller.php:

Code:
function Controller()
    {    
        parent::CI_Base();
        $this->_ci_initialize();
        log_message('debug', "Controller Class Initialized");
        
        // A little hack
        require_once(constant('APPPATH').'/AuthClass.php');
        $acl = new AuthClass;
        $acl->is_authorized();
    }

And now when parent::Controller is executed in each controller I have simple pre_controller hook. Works great for me.

Thanks for responses.