【问题标题】:CodeIgniter load database in Controller or Model?CodeIgniter 在控制器或模型中加载数据库?
【发布时间】:2016-04-27 04:57:28
【问题描述】:

型号:

class Users_model extends CI_Model {
    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function get_all_users() {
        return $this->db->get('users');
    }
}

控制器:

class Users extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('security');
        $this->load->model('Users_model');
    }

    public function index() {
        redirect('users/view_users');
    }

    public function view_users() {
        $data['query'] = $this->Users_model->get_all_users();
        $this->load->view('users/view_all_users', $data);
    }
}

我的问题是我应该把 $this->load->database 放在哪里?在模型或构造函数中?如果可能的话告诉我为什么? 还有一个问题,如果我省略了 $this->load->database,则会显示错误

“未定义属性:Users::$db”。我期待“未定义的属性: 用户模型::$db"。

这是为什么呢?它在控制器或模型中都在寻找 $db 吗? 谢谢你。 注意:我可以很好地连接到数据库。我实际上要问的是我是否想使用 $this->load->database()。我应该把它放在哪里?控制器还是模型?为什么?

【问题讨论】:

  • database.php文件中的数据库信息配置了吗?配置文件位于application/config/database.php 更多信息请访问here

标签: php database codeigniter model


【解决方案1】:

转到application/config/autoload.php 中的autoload.php 并添加此内容

$autoload['libraries'] = array('database'); // add database in array(now you dont need to load database at anywhere in project)

在database.php中进行数据库连接设置,文件位于application/config/database.php

现在试试这个

 class Users_model extends CI_Model {
  function __construct() {
    parent::__construct();
    //$this->load->database(); <----remove this
  }

  public function get_all_users() {
    return $this->db->get('users');
  }
}

【讨论】:

    【解决方案2】:

    您可以将数据库加载到控制器中,也可以将其加载到模型中。

    没有太大区别,只是数据库中的所有交互都在模型中更加简洁明了控制器连接视图和模型。

    这里的控制器就像买卖双方之间的中间人。

    在控制器中加载数据库

    <?php 
       class Test_controller extends CI_Controller {
    
            public function __construct()
          {
              parent::__construct();
              $this->load->database('default');
              $this->load->model('test_model');
          }
    
          public function index()
          {
            $this->db->from('test');
            $query = $this->db->get();
            echo "<title>CodeIgniter SQlite</title>";
            $data['db']=$query->result();
            //print_r($data['db']);
            $count = count($data['db']);
            echo "Row count: $count rows<br>";
            for ($i=0; $i < $count; $i++) { 
              echo $data['db'][$i]->text." ";
            }
          }
       }
    ?>
    

    在模型中加载数据库

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Test_model extends CI_Model
    {
    
        public $table1 = 'test';
        public $table2 = '';
    
    
    
        public function __construct()
        {
            parent::__construct();
            $this->load->database('default');
        }
    
        public function check_db()
        {
            $this->db->from($this->table1);
            $query = $this->db->get();
            return $query->result();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 2014-01-29
      • 2012-04-02
      • 1970-01-01
      相关资源
      最近更新 更多