Welcome Guest, Not a member yet? Register   Sign In
hooks + database(not able to access database)
#1

[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?
#2

[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

    }
}
?>
#3

[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.
#4

[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

    }
}
#5

[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');
#6

[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
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB