Welcome Guest, Not a member yet? Register   Sign In
Access Database Class
#1

[eluser]ranjudsokomora[/eluser]
Hello CI Coders,
I am trying to initialize the database class inside of my rewrite of the router class.
Is there a way to do this? Here is my code so far but I don't know how to take it from here, because this produces an error.

Code:
$CI    =   & load_class('Loader');
$CI    =   new CI_Loader();
$CI->database();
#2

[eluser]Alex-Tunbridge[/eluser]
What are you using? Library, Model, Helper, Controller?
#3

[eluser]ranjudsokomora[/eluser]
Hello Alex-Tunbridge,
I am actually rewriting the router class. To accomplish this I have created a file in ./system/application/libraries. I have named the file MY_router.php

At this point all I want to rewrite is the _set_routing function. I want to use the CI DB because of it's support for multiple SQL server types.

Here is my code so var

Code:
class MY_Router extends CI_Router
{
    
    function MY_Router()
    {
      parent::CI_Router();
    }
    
    function _set_routing()
    {
      $CI   =   & load_class('Loader');
      $CI   =   new CI_Loader();
      $CI->database();
    }
}

I want to completely rewrite how the routes are built.
#4

[eluser]Alex-Tunbridge[/eluser]
I believe this is how its done actually.
Code:
class MY_Router extends CI_Router
{
    
    function MY_Router()
    {
      parent::CI_Router();
      $this->CI =& get_instance();
    }
    
    function _set_routing()
    {
      $this->CI->db->get('my_table');
    }
}
#5

[eluser]ranjudsokomora[/eluser]
Alex-Tunbridge,
I get a few errors when I do that
Quote:Severity: Notice
Message: Undefined property: MY_Router::$CI
Filename: libraries/my_router.php

Quote:Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/my_router.php

I know that the &get;_instance function isn't initialized until after the config,uri,router,output,input and language classes are loaded... there for I believe (but may be wrong) that any reference to the function &get;_instance would fail
#6

[eluser]WanWizard[/eluser]
Correct, and it's one of the reasons it's very difficult (I wouldn't like to use the word impossible) to have database driven routing via CI's database library.
People that do this usually resort to using PHP's native database functions, giving up portability.

I decided to go the other way, I have a fixed route that routes all URI's to the same controller. That controller has full database access, processes the URI and loads the controller linked to that URI.
#7

[eluser]ranjudsokomora[/eluser]
Got IT! Database driven routes, with cache!

http://ellislab.com/forums/viewthread/156342/




Theme © iAndrew 2016 - Forum software by © MyBB