CodeIgniter and autoloading the database library

后端 未结 7 931
小鲜肉
小鲜肉 2021-01-21 04:19

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

In the model, if I do

$this->load->database();

then a query such

相关标签:
7条回答
  • 2021-01-21 04:36

    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!

    0 讨论(0)
  • 2021-01-21 04:37

    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.

    0 讨论(0)
  • 2021-01-21 04:40

    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.

    0 讨论(0)
  • 2021-01-21 04:52

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

    0 讨论(0)
  • 2021-01-21 04:54

    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');

    0 讨论(0)
  • 2021-01-21 04:59

    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.

    0 讨论(0)
提交回复
热议问题