code_junkie

Complex Find in Cake PHP

Nov 11th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. SELECT *
  2. FROM `tips`
  3. JOIN users ON users.id = tips.user_id
  4. JOIN friends_users ON tips.user_id = friends_users.friend_id
  5. WHERE (friends_users.user_id =2 or tips.user_id=2)
  6. LIMIT 0 , 30
  7.  
  8. function findTipsByUserAndFriends($userId) {
  9. //FriendsUser is automagically created by CakePHP "with" association
  10. $conditions = array('FriendsUser.user_id'=>$userId);
  11. $fields = 'FriendsUser.friend_id';
  12. //get a list friend ids for the given user
  13. $friendIds = $this->Tip->User->FriendsUser->find('list', compact('conditions', 'fields'));
  14. //get list of all userIds for whom you want the tips
  15. $userIds = array($userId) + $friendIds;
  16. $conditions = array('Tip.user_id'=>$userIds);
  17. $tips = $this->Tip->find('all', compact('conditions'));
  18. return $tips;
  19. }
  20.  
  21. $aCond = array(
  22. 'fields' => array(
  23. 'Discount.*'
  24. ),
  25. 'contain' => array(
  26. 'Event' => array(
  27. 'conditions' => array('Event.id' => $iEventId)
  28. )
  29. )
  30. );
  31. $aBulkDiscounts = $this->Discount->find('first', $aCond);
Add Comment
Please, Sign In to add comment