0

I have two tables which are related as HABTM:

  • Groups(id, name)
  • Streams(id, Stream)

Connecting table:

  • groups_streams(id, group_id, stream_id)

There is another model called Users which HasOne Group.

From the User controller I am trying to get the streams related to the User's Group.

$streams = $this->User->Group->find('list', array('conditions' => array(`User.group_id` => 2)));

I am doing this, but I'm getting a MySQL error:

SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= 2' at line 1

SQL Query:

SELECT `Group`.`id` FROM `groups` AS `Group` WHERE = 2  

I'm confused :(

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
Harsha M V
  • 54,075
  • 125
  • 354
  • 529

2 Answers2

2

You need to wrap User.group_id in quotes within your 'conditions' array:

$streams = $this->User->Group->find('list', array(
    'conditions' => array('User.group_id' => 2)
));
Daniel Wright
  • 4,584
  • 2
  • 27
  • 28
1

I would rather go with:

$streams = $this->Stream->Group->find('list', array('conditions' => array('Group.id => 2));
pawelmysior
  • 3,190
  • 3
  • 28
  • 37