CodeIgniter Forums
hooks + database(not able to access database) - 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: hooks + database(not able to access database) (/showthread.php?tid=5293)



hooks + database(not able to access database) - El Forum - 01-15-2008

[eluser]nirbhab[/eluser]
hello everybuddy,
i have a small problem. i am not able to access database in hook.

config.php
Code:
$config['enable_hooks'] = TRUE;

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

getsystem.php

Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class getSystem
{
    var $CI;
    function check()
    {
        define('MAIL',TRUE); //this is running
        $this->CI =& get_instance(); got instance of CI
                //$query = $this->CI->db->get('laf_admin'); //if uncommented, gives error

    }
}
?>

wht to do?


hooks + database(not able to access database) - El Forum - 01-16-2008

[eluser]ejangi[/eluser]
You probably need to load the database stuff explicitly as the DB stuff normally gets loaded during the Controller (whereas you're doing this hook pre-controller):
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class getSystem
{
    var $CI;
    function check()
    {
       define('MAIL',TRUE); //this is running
       $this->CI =& get_instance(); got instance of CI
       $this->CI->load->database();
       //$query = $this->CI->db->get('laf_admin'); //if uncommented, gives error

    }
}
?>



hooks + database(not able to access database) - El Forum - 01-16-2008

[eluser]nirbhab[/eluser]
i did the following but it shows error in loading.
Code:
$this->CI->load->database();
rather than that i extended the hooks class
means
Code:
class getSystem extends Controller

is it right to perform, i can now use all my ci class by default.


hooks + database(not able to access database) - El Forum - 01-16-2008

[eluser]tonanbarbarian[/eluser]
umm there may be a syntax error in those examples

the get_instance looks like it was meant to have a comment after it, but it is not commented correctly

try this
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class getSystem
{
    var $CI;
    function check()
    {
       define('MAIL',TRUE); //this is running
       $this->CI =& get_instance(); //got instance of CI
       $this->CI->load->database();
       //$query = $this->CI->db->get('laf_admin'); //if uncommented, gives error

    }
}



hooks + database(not able to access database) - El Forum - 01-16-2008

[eluser]nirbhab[/eluser]
I pasted the above code:
n guess wht

Quote:A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: hooks/getsystem.php

Line Number: 10
Fatal error: Call to a member function database() on a non-object in D:\www\vhosts\localhost\hook\system\application\hooks\getsystem.php on line 10



one more thing to mention:

i am auto loading database:
autoload.php
Code:
$autoload['libraries'] = array('database');



hooks + database(not able to access database) - El Forum - 01-16-2008

[eluser]tonanbarbarian[/eluser]
The problem is you are using a pre-controller hook. At that point the controller does not technically exist yet.
try using a post_controller_constructor hook instead and it should work


hooks + database(not able to access database) - El Forum - 01-16-2008

[eluser]nirbhab[/eluser]
nice idea! thanx dear it worked well :-)