【发布时间】: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