• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loading Config from Database

#1
[eluser]Arrow768[/eluser]
Hi,
I am trying to load my additional config from a Database.

After some Google´ing I found the Hook-Method from vbsaltydog. (http://ellislab.com/forums/viewreply/967955/)

When I load my page, I get the following Error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: hooks/config.php

Line Number: 15

Fatal error: Call to a member function get() on a non-object in E:\XAMPP\htdocs\Donator-Interface\application\hooks\config.php on line 15


What I have done until now:

I load the database library with autoload
I have enabled the Hooks in the config.php file
I tried to make a var_dump of $CI (result is NULL)

It would be great if you could help me with my problem.

This are my files:
hooks/config.php
Code:
<?php if ( !defined('BASEPATH') ) exit('No direct script access allowed');

function pre_controller()
{

    $CI = & get_instance( );
    var_dump($CI);

    $results = $CI->db->get('settings')->result();

    foreach ($results as $setting) {

        $CI->config->set_item($setting->id, $setting->value);

    }//End of foreach

}
?>

config/hooks.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$hook['pre_controller'] = array(
                                'class'    => '',
                                'function' => 'pre_controller',
                                'filename' => 'config.php',
                                'filepath' => 'hooks',
                                );

/* End of file hooks.php */
/* Location: ./application/config/hooks.php */

log-file
Code:
DEBUG - 2012-09-07 19:09:23 --> Config Class Initialized
DEBUG - 2012-09-07 19:09:23 --> Hooks Class Initialized
DEBUG - 2012-09-07 19:09:23 --> Utf8 Class Initialized
DEBUG - 2012-09-07 19:09:23 --> UTF-8 Support Enabled
DEBUG - 2012-09-07 19:09:23 --> URI Class Initialized
DEBUG - 2012-09-07 19:09:23 --> Router Class Initialized
DEBUG - 2012-09-07 19:09:23 --> Output Class Initialized
DEBUG - 2012-09-07 19:09:23 --> Security Class Initialized
DEBUG - 2012-09-07 19:09:23 --> Input Class Initialized
DEBUG - 2012-09-07 19:09:23 --> Global POST and COOKIE data sanitized
DEBUG - 2012-09-07 19:09:23 --> Language Class Initialized
ERROR - 2012-09-07 19:09:23 --> Severity: Notice  --> Trying to get property of non-object E:\XAMPP\htdocs\Donator-Interface\application\hooks\config.php 15

#2
[eluser]Madigan[/eluser]
I'm an utter PHP newbie, but shouldn't it be
Code:
$CI =& get_instance( );
instead of
Code:
$CI = & get_instance( );
Or can PHP see past the space?

#3
[eluser]Arrow768[/eluser]
The Space is not the Problem

I have tried it out, but I still get the same Error.

thanks for trying

#4
[eluser]PhilTem[/eluser]
I think the problem is: Hooks are executed very early, so you might not have the DB class loaded (do you autoload it or in the controller?).

You might also want to try

Code:
function pre_controller()
{
    global $DB;
    var_dump($DB);
}
and just see if you got a DB object available Wink

#5
[eluser]Arrow768[/eluser]
I solved the Problem:
hooks/Load_items.php
Code:
<?php if ( !defined('BASEPATH') ) exit('No direct script access allowed');

/*
This hook function is responsible for reading all of the settings from
the database into the config array so they can be accessed in controllers
and views with $this->config->item('whatever'); or config_item('whatever');
*/

class MY_Load extends CI_Controller{

    function Load_items()
    {

        $CI =& get_instance();  
        //var_dump($CI);

        $results = $CI->db->get('settings')->result();

        foreach ($results as $setting) {

            $CI->config->set_item($setting->id, $setting->value);

        }//End of foreach

    }
}
?>

config/hooks.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files.  Please see the user guide for info:
|
| http://ellislab.com/codeigniter/user-guide/general/hooks.html
|
*/

$hook['pre_controller'] = array(
                                'class'    => 'MY_Load',
                                'function' => 'Load_items',
                                'filename' => 'Load_items.php',
                                'filepath' => 'hooks',
                                );

/* End of file hooks.php */
/* Location: ./application/config/hooks.php */

#6
[eluser]Unknown[/eluser]
For my hook to work:
I extended the CI_Hooks, not the CI_Controller.
I used post_controller_constructor in order to get the db, etc.
I set enable_hooks to TRUE in the config.php
Also, I am using version 1.7.2 which is worth mentioning.
Cheers. -M



Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.