How to limit the fields of the associated models using find method

一曲冷凌霜 提交于 2019-12-25 01:32:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!