CodeIgniter查询结果(类)包括非属性字段

Say I declare a class:

class Abc extends CI_Model {
  public $name;
  public function __construct() {
    parent::__construct();
  }
}

Then I do

$this->db->get('my_table')->result('Abc')`

The result is array like (json_encoded):

[{"id": 1, "name": "John", "age": 25},
 {"id": 2, "name": "Diana", "age": 35},
 {"id": 3, "name": "Robert", "age": 27}]

Shouldn't each of array items contains only "name", just like declared in class Abc? Since "id" and "age" fields come from the database table. Thanks.

Codeigniter models don't work like that since it does not have an ORM (which I personally think is one of the many flaws of CI as it does not really enforce MVC unlike Laravel's eloquent or Symfony's Doctrine). Instead they make use of what they call is an implementation of the active record pattern.

If you want to get just the name, you have to call:

   $this->db->select('name');

before you call get()

CI models are simply more of a place where you can put together related database operations. I think a repository is a more appropriate name.

Codeigniter Help for Active record class

You should use

`$this->db->select('title, content, date');
$query = $this->db->get('mytable');`

Produces:

`SELECT title, content, date FROM mytable`