code_junkie

CakePHP n to n relation on a third table

Nov 11th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. id
  2. make_id
  3. model_id
  4. ...
  5.  
  6. var $uses = array('Make', 'Model');
  7.  
  8. $this->Make->id = 5; // My Make
  9.  
  10. $this->Make->find(); // Returns only the make I want with it's Models (HABTM)
  11. $this->Model->find('list'); // Returns ALL models
  12. $this->Make->Model->find('list'); // Returns ALL models
  13.  
  14. $makeArray = $this->Make->find();
  15. foreach ($makeArray['Model'] as $model) {
  16. $modelList[] = $model['title'];
  17. }
  18. $this->set('models', $models)
  19.  
  20. SELECT * FROM makes, models, products WHERE make_id = 5
  21.  
  22. SELECT models.title FROM models
  23. INNER JOIN products
  24. ON products.model_id = models.model_id
  25. AND products.make_id = 5
  26.  
  27. $this->Product->find('all', array('conditions' => array('make_id' => 5)));
  28.  
  29. $this->ModelB->find('first', array('conditions' => array('field' => $condition)));
  30. array(
  31. ['ModelB'] => array( ... ),
  32. ['ModelA'] => array(
  33. [0] => array(
  34. 'id' => 1
  35. )
  36. )
  37.  
  38. $this->ModelA->find('all', array('conditions' => array('id' => $model_a_ids)));
  39.  
  40. $habtm = " ...
  41. 'with' => 'MakeModel',
  42. ... ";
Add Comment
Please, Sign In to add comment