Advertisement
Guest User

many_many

a guest
Jun 20th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <?php
  2.  
  3. class PostPage extends Page {
  4.  
  5. public static $db = array(
  6. 'Title' => 'Text'
  7. );
  8.  
  9. public static $many_many = array(
  10. 'Authors' => 'AuthorPage'
  11. );
  12.  
  13. public function getCMSFields() {
  14.  
  15. $fields = parent::getCMSFields();
  16.  
  17.  
  18. // Get all Campuses (locations)
  19. $campuses = AuthorPage::get();
  20.  
  21. // Create a map(array) with the format array(ID=>Title). This is the default as per
  22. // http://api.silverstripe.org/3.0/class-DataList.html#_map
  23. $map = $campuses->map();
  24.  
  25. // Create a CheckboxSetField to select course locations
  26. $courseLocations = new CheckboxSetField('Authors', 'Choose course locations', $map);
  27.  
  28. // Add the CheckboxSetField to the CMS
  29. $fields->addFieldToTab('Root.Main', $courseLocations, 'Content');
  30. $fields->addFieldToTab('Root.Main', new TextField('Title'), 'Content');
  31.  
  32.  
  33. return $fields;
  34. }
  35. }
  36.  
  37. class AuthorPage extends Page {
  38.  
  39. public static $db = array(
  40. 'Name' => 'Text'
  41. );
  42.  
  43. public static $many_many = array(
  44. 'Posts' => 'PostPage'
  45. );
  46.  
  47. public function getCMSFields() {
  48.  
  49. $fields = parent::getCMSFields();
  50.  
  51. $fields->addFieldToTab('Root.Main', new TextField('Name'), 'Content');
  52.  
  53.  
  54. return $fields;
  55. }
  56. }
  57.  
  58. class HomePage_Controller extends Page_Controller {
  59.  
  60. public function filterStuff() {
  61. $results = PostPage::get()->filter(array('Authors.ID:ExactMatch' => 1));
  62. return $results->renderWith('SomeTemplate');
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement