Welcome Guest, Not a member yet? Register   Sign In
How to get database functionality in core classes
#1

[eluser]d1a8lo24[/eluser]
Hello everyone, new to the forums they have been a great help when building applications with CI.

So I need some help in getting the database to work on the core classes specially in the routing class.

Now I know that if you want to use the CI super global all you need to do is call the "get_instance()" that one is very easy and it works find when extending the libraries but not the core classes.

Now I know the core classes work a different way because of the way they are being loaded so is it possible to get database functionality at this stage?

And what would I need to do to implement it.

If someone needs a reason on why I would want the database functionality well, is because I think it will be a lot easier to create dynamic routing by just extending the class and having the database create the array.

Now I have everything that I need I just need the database to work at this stage.

Thanks in advance.
#2

[eluser]InsiteFX[/eluser]
Code:
private $CI;

// Set the CodeIgniter Super Object to a local variable for use later.
$this->CI =& get_instance();

// Load the database library

// to access use
$this->CI->db->function();

InsiteFX
#3

[eluser]d1a8lo24[/eluser]
That is the first thing that I mention that the get_instance() function to get the super global doesn't work in the "core classes" it works fine in the libraries, but not at this stage because of the way the classes are being loaded.

I did find 2 topics after an intensive search one is to load the database manually and the other is to create a controller that will create a routes file with your new routes and then include the file with the config routes.

The first approach is kind of what I was looking for but its too much work because of not being able to use the CI database class, the second approach is what I'm using at the moment whenever I create a new page in the application a new routes file is created and as CI's reloads it loads all the new files. Not the best but it works.

By the way your code will produce this error in the routes core class.

Fatal error: Class 'CI_Controller' not found in C:\wamp\www2\system\core\CodeIgniter.php on line 210

Here is what I have right now taken from the following topic:
http://ellislab.com/forums/viewthread/154170/

This is a function in my model
Code:
function _create_routes()
    {
        // Load the helper file
        $this->load->helper('file');
        
        // Turn on Query Caching
        $this->db->cache_on();
        
        // Run your query to get your routes
        // This will be different depending on your project and database
        $query = $this->db
                      ->get('your_routes');
                      
        if($query->num_rows() > 0)
        {
            // This will be written at the beggining of the file
            $data[] = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');";
            
            // Loop through each route in your database and assign the keys and values.
            foreach($query->result() as $route)
            {
                // In my case L'm using one controller to take care of the pages being display
                // If you're using different controllers for different pages make sure to assign correctly
                $data[] = '$route["' . $route->route  . '"] = "main/page/index";';
            }
            
            // Separate each entry in the array with a new line break
            $output = implode("\n", $data);

            // Write the file to your directory.
            write_file(BASEPATH . "cache/routes.php", $output);
            
            return;
        }
        
        // Make sure to write a file if no records are return in order not to throw an error when including the file.
        write_file(BASEPATH."cache/routes.php", "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');");
        
        return;
    }

Once the routes file has been created just include the file in your config routes file.

Code:
$route['default_controller']                     = "main";
$route['scaffolding_trigger']                     = "";

// Load the routes.php file.
@include(BASEPATH.'cache/routes'.EXT);

In my case I use query caching for performance, and i use the cache folder in the system folder to write everything in it from the routes file and any other temporary files. This way I can delete everything when ever I update or create a new page.
Just call one function using the file helper and calling the delete_files() function.

At this point this is the best approach to dynamic routing and thanks again to neillyons.info for his code sample at:
http://ellislab.com/forums/viewthread/154170/
#4

[eluser]InsiteFX[/eluser]
You can look at the Loader.php file and the function database();

This is how CodeIgniter loads then etc.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB