我真的可以在不创建模型的新实例的情况下进行数据库查询吗?
例如,在我的 Controller 中我有 (CONTROLLER)
Name_Model::get_all_from_another_table();
在我的 name_model.php(模型)中
public static function get_all_from_another_table() {
$this->db->query('select * from another_table');
return $x;
}
这种情况发生在创建新模型实例之前加载具有预定义值的表。
以上代码返回:
Fatal error: Using $this when not in object context
最佳答案
先试试。
Controller :
$this->load->model( 'name_model' );
$data = $this->name_model->get_all_from_another_table()->result();
print_r( $data ); //just for testing...
型号:
//Model name: name_model.php
public static function get_all_from_another_table() {
$this->db->query('select * from another_table');
return $x;
}
关于php - CodeIgniter 上没有模型对象的数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44080407/