0

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Josh Randall
  • 1,284
  • 4
  • 18
  • 31

1 Answers1

5

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')));
Shaz MJ
  • 1,785
  • 1
  • 12
  • 25