code_junkie

CakePHP find() based on HABTM relationships

Nov 12th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. var $hasAndBelongsToMany = array(
  2. 'Course' => array(
  3. 'className' => 'Course',
  4. 'joinTable' => 'course_members',
  5. 'foreignKey' => 'user_id',
  6. 'associationForeignKey' => 'course_id',
  7. 'conditions' => '',
  8. 'order' => '',
  9. 'limit' => '',
  10. 'uniq' => false,
  11. 'finderQuery' => '',
  12. 'deleteQuery' => '',
  13. 'insertQuery' => ''
  14. ),
  15. 'School' => array(
  16. 'className' => 'School',
  17. 'joinTable' => 'school_members',
  18. 'foreignKey' => 'user_id',
  19. 'associationForeignKey' => 'school_id',
  20. 'conditions' => '',
  21. 'order' => '',
  22. 'limit' => '',
  23. 'uniq' => false,
  24. 'finderQuery' => '',
  25. 'deleteQuery' => '',
  26. 'insertQuery' => ''
  27. ),
  28. 'Team' => array(
  29. 'className' => 'Team',
  30. 'joinTable' => 'team_members',
  31. 'foreignKey' => 'user_id',
  32. 'associationForeignKey' => 'team_id',
  33. 'conditions' => '',
  34. 'order' => '',
  35. 'limit' => '',
  36. 'uniq' => false,
  37. 'finderQuery' => '',
  38. 'deleteQuery' => '',
  39. 'insertQuery' => ''
  40. )
  41. );
  42.  
  43. SELECT
  44. `User`.`id`, `User`.`username`, `User`.`password`, `User`.`firstName`, `User`.`lastName`, `User`.`email
  45.  
  46. `, `User`.`phone`, `User`.`streetAddress`, `User`.`city`, `User`.`province`, `User`.`country`, `User
  47.  
  48. `.`postal`, `User`.`userlevel`, `User`.`modified`, `User`.`created`, `User`.`deleted`, `User`.`deleted_date
  49.  
  50. ` FROM `users` AS `User` WHERE `User`.`id` = 6 AND `School`.`name` LIKE '%Test%' LIMIT 1
  51.  
  52. $this->Recipe->Tag->find('all', array(
  53. 'conditions' => array('Tag.name' => 'Dessert')));
  54.  
  55. $this->Recipe->bindModel(array('hasOne' => array('RecipesTag')));
  56. $this->Recipe->find('all', array(
  57. 'fields' => array('Recipe.*'),
  58. 'conditions' => array('RecipesTag.tag_id' => 124) // id of Dessert
  59. ));
  60.  
  61. $this->Recipe->bindModel(array('hasOne' => array('RecipesTag',
  62. 'FilterTag' => array(
  63. 'className' => 'Tag',
  64. 'foreignKey' => false,
  65. 'conditions' => array('FilterTag.id = RecipesTag.tag_id')
  66. ))));
  67. $this->Recipe->find('all', array(
  68. 'fields' => array('Recipe.*'),
  69. 'conditions' => array('FilterTag.name' => 'Dessert')
  70. ));
Add Comment
Please, Sign In to add comment