开发者

CodeIgniter and autoloading the database library

I've got an issue connecting to the database with CodeIgniter.

In the model, if I do

$this->load->database();
开发者_如何学Python

then a query such as

$query = $this->db->get('articles', 10);

works and returns data. However, when I remove the load->database line and try autoloading the database library, using

$autoload['libraries'] = array('database');

in application/config/autoload.php, the above query fails. As it works when I explicitly load the library, it's not a problem with my database connection. I'm using CodeIgniter 2.0.2 (the current latest release).

As most of my pages use the database I'd like to autoload it to avoid having to load it anually in each model. Have I misunderstood the documentation regarding loading the database library or am I doing something wrong?


I know I am probably just answering a dead question but i had the same problem.

if you are using eclipse pdt or similar IDE and modified the system>core>model.php by adding some variables just before the constructor to get code completion then the autoload doesnt work

i dont know why but the fact is it doesnt work. I restored the core>model.php to original and autoload works fine. I dont get autoload now and its really a bad experience for new coder to CI.

If you have a workaround please comment so.


This is just an alternative if you still can't resolve the issue. Just extend the CI_Model and auto load the database library in the constructor. Then you can just make all other models inherit from that base class.

Example:

<?php 
    class My_Model extends CI_Model{
        public function __construct(){
            parent::__construct();
             $this->load->database();
         }
     }

And the all others will start like :

  <?php
       class Some_Model extends MY_Model{}

This method has the other benefit that you don't have to include the loading code line in every model you create. Also you can easily push shared functionalities among models into the parent MY_Model class.

Hope it helps!


This is my database.php from my application/config/ directory

$active_group = 'default';   
$active_record = TRUE;        
$db['default']['hostname'] = 'mydbhost';   
$db['default']['username'] = 'myusername';    
$db['default']['password'] = 'mypassword';    
$db['default']['database'] = 'mydatabase'; 
$db['default']['dbdriver'] = 'mysql';    
$db['default']['dbprefix'] = '';    
$db['default']['pconnect'] = TRUE;    
$db['default']['db_debug'] = TRUE;   
$db['default']['cache_on'] = FALSE;   
$db['default']['cachedir'] = '';    
$db['default']['char_set'] = 'utf8';    
$db['default']['dbcollat'] = 'utf8_general_ci';    
$db['default']['swap_pre'] = '';    
$db['default']['autoinit'] = TRUE;    
$db['default']['stricton'] = FALSE;

Check yours looks like this and rerun through the user guide at http://codeigniter.com/user_guide/database/configuration.html and reread the comments in the autoload.php file in the config directory.

The relevant line in mine reads:

$autoload['libraries'] = array('database', 'form_validation', 'session');

You should be loading the db library like this:

$this->load->library('database');


The only thing I can think of is in your application/config/database.php file, the following line is set to false, instead of true.

//['autoinit'] Whether or not to automatically initialize the database.
$db['default']['autoinit'] = TRUE;

Everything else looks ok.


Shababhsiduque,

Just you we're right, at least in my case!

The workaround to getting the ide to work is creating the reference variables in ANOTHER CI project.

That is leave the system model as it is in the current project but modify it in another project.

Then change the php buildpath (project->properties->phpbuildpath) to the project with the variables.

Note this are settings for Aptana Studio 3.

Worked for me.


You could try PHP Autoload Class Magic function in this case.

    function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'libraries/'. $class . '.php' );
    }
}

This code must copied inside the config.php (after the last line) within Application folder.

This code will automatically load libraries for you when its needed.

It might work. Happy coding.


In my case the problem was because there was another occurrence of $autoload['libraries'] inside the same file and it reset the array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜