问题
I have user model and articles model. An User hasMany articles. So when I query for a user, all fields for article table is retrieved. I want to limit it just title of articles.
$user = $this->User->find('all', array('conditions' => array('User.id' => $id), 'fields' => array('User.firstName', 'Article.title')));
The fields works fine for user model. But it does not work for associated models. throws error
SQL Error: 1054: Unknown column 'Article.title' in 'field list'
I appreciate any help.
回答1:
You're better of using Containable, and it's just as easy:
$this->User->Behaviors->attach('Containable');
$user = $this->User->find('all', array('conditions' => array('User.id' => $id), 'contain' => array('Article.title'), 'fields' => array('User.firstName')));
来源:https://stackoverflow.com/questions/7121988/how-to-limit-the-fields-of-the-associated-models-using-find-method