Sunday, December 9, 2012

Conditional Model Query

Hi there,

I have a small application that has three tables.  users, roles, and role_users.  The role_users table is a join table as a user can have multiple roles.   In my UsersController class I am trying to get a count for the number of roles the current user has where there is a enduserid = 1.   

public function hasEndUser(){

$this->loadModel('RoleUser');

$userId = $this->Auth->user('id'); 

$conditions = array('RoleUser.user_id ='=>$userId,'RoleUser.enduserid ='=>1);

$result = $this->RoleUser->find('count',$conditions);

debug($result);

}

For whatever reason, this is the SQL that I am getting back:

SELECT COUNT(*) AS `count` FROM `bridges`.`role_users` AS `RoleUser` LEFT JOIN `bridges`.`users` AS `User` ON (`RoleUser`.`user_id` = `User`.`id`) LEFT JOIN `bridges`.`roles` AS `Role` ON (`RoleUser`.`role_id` = `Role`.`id`) WHERE 1 = 1


As a result, I am also getting the wrong count.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: