Welcome Guest, Not a member yet? Register   Sign In
Loading a Model inside of a config file to pull configurations from DB and store in $config array
#1

[eluser]Unknown[/eluser]
I'm refitting an existing website to work with codeigniter. The current system stores the navigation config in a single table cell using XML in this format:

Code:
<NAVIGATION>
      <item1>
        <id>1</id>
        <name>Home</name>
        &lt;link&gt;&lt;/link>
        <parent>0</parent>
        <target>_self</target>
        <rel>follow</rel>
        <menu>main</menu>
      </item1>
      <item2>
        <id>2</id>
        <name>Some Page</name>
        &lt;link&gt;some-page&lt;/link&gt;
        <parent>0</parent>
        <target></target>
        <rel>follow</rel>
        <menu>main</menu>
      </item2>
    </NAVIGATION>

I'm not sure what the original developer had in mind, but for reasons which will remain unexplained I would like to maintain this format for now.

I created a navigation model which pulls this XML from the db and converts it into an array.

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Navigation_model extends CI_Model
    {
    
     function __construct()
     {
      parent::__construct();
     }
    
     function get_navigation()
     {
      $this->db->select('pages.block');
      $this->db->from('pages');
      $this->db->join('style','style.styleID = pages.styleID','left');
      $this->db->where('style.name','data');
      $this->db->where('pages.locID',$this->config->item('splitID'));
      $this->db->like('pages.block','<NAVIGATION>');
      $navigation_query = $this->db->get();
      $navigation_row = $navigation_query->row_array();
      return xml2array($navigation_row['block']);
     }
    }
    
    /* End of file navigation_model.php */
    /* Location: ./application/model/navigation_model.php */

What I'm trying to do, is to load this model in a config file called navigation.php and load the navigation array into the global $config array. I tried both autoloading the model and loading inside navigation.php but to no avail.

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    $this->load->model('Navigation_model');
    var_dump($this->Navigation_model->get_navigation());
    
    /* End of file navigation.php */
    /* Location: ./application/config/navigation.php */

I keep getting the following error:

Quote:Severity: Notice

Message: Undefined property: CI_Config::$load

Filename: config/navigation.php

Line Number: 3

How can I best accomplish what I am trying to do?
#2

[eluser]weboap[/eluser]
Code:
//this won't work in a config file.
//config files are manly for vars/arrays/constants.....

$this->load->model('Navigation_model');
var_dump($this->Navigation_model->get_navigation());


can't you use MY_Controller and extend your controllers to it, then place
model load and nav stuff there in a function that you can load in the MY_Controller class construct
will help load other custom common stuff.....

#3

[eluser]CroNiX[/eluser]
In the CI bootstrap, the configs are loaded way before the database (which isn't even required), so no models are available to the config. You can get around that by saving your xml files to the filesystem instead of the db, or use a MY_Controller as @weboap suggested.




Theme © iAndrew 2016 - Forum software by © MyBB