Php CodeIgniter查询结果(类)包括非属性字段
假设我声明一个类:Php CodeIgniter查询结果(类)包括非属性字段,php,database,codeigniter,Php,Database,Codeigniter,假设我声明一个类: class Abc extends CI_Model { public $name; public function __construct() { parent::__construct(); } } 那我会的 $this->db->get('my_table')->result('Abc')` 结果类似于数组(json_编码): 每个数组项不应该只包含“name”,就像在类Abc中声明的那样吗?因为“id”和“age”字段来自数据
class Abc extends CI_Model {
public $name;
public function __construct() {
parent::__construct();
}
}
那我会的
$this->db->get('my_table')->result('Abc')`
结果类似于数组(json_编码):
每个数组项不应该只包含“name”,就像在类Abc中声明的那样吗?因为“id”和“age”字段来自数据库表。谢谢。Codeigniter模型不是这样工作的,因为它没有ORM(我个人认为这是CI的许多缺陷之一,因为它与Laravel的雄辩或Symfony的学说不同,没有真正实施MVC)。相反,他们利用了他们所谓的活动记录模式的实现 如果您只想知道姓名,您必须拨打:
$this->db->select('name');
在调用get()之前
CI模型更像是一个可以组合相关数据库操作的地方。我认为存储库是一个更合适的名称
你应该使用
`$this->db->select('title, content, date');
$query = $this->db->get('mytable');`
产生:
`SELECT title, content, date FROM mytable`
`SELECT title, content, date FROM mytable`