School.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * School
  4. *
  5. * Shows an example of a school system using the ORM
  6. *
  7. * @package MicroMVC
  8. * @author David Pennington
  9. * @copyright (c) 2011 MicroMVC Framework
  10. * @license http://micromvc.com/license
  11. ********************************** 80 Columns *********************************
  12. */
  13. namespace Controller;
  14. class School extends \MyController
  15. {
  16. public function run()
  17. {
  18. $this->load_database();
  19. // You can over-ride this in certain models if needed,
  20. // allowing you to use multiple databases.
  21. // Model_Name::$db = new DB(config('other_database'));
  22. // New Dorm
  23. $d = new \Model\Dorm();
  24. $d->name = 'Dorm 1';
  25. $d->save();
  26. // New Student in Dorm
  27. $s1 = new \Model\Student();
  28. $s1->name = 'Mary';
  29. $s1->dorm_id = $d->id;
  30. $s1->save();
  31. // New Student in Dorm
  32. $s2 = new \Model\Student();
  33. $s2->name = 'Jane';
  34. $s2->dorm_id = $d->id;
  35. $s2->save();
  36. // New Car for student
  37. $c = new \Model\Car();
  38. $c->name = 'Truck';
  39. $c->student_id = $s1->id;
  40. $c->save(); // Insert
  41. $c->name = $s1->name. '\'s Truck'; // Change car name
  42. $c->save(); // Update
  43. // New Softball club
  44. $c = new \Model\Club();
  45. $c->name = 'Softball';
  46. $c->save();
  47. // Mary is in softball
  48. $m = new \Model\Membership();
  49. $m->club_id = $c->id;
  50. $m->student_id = $s1->id;
  51. $m->save();
  52. // Jane is in softball
  53. $m = new \Model\Membership();
  54. $m->club_id = $c->id;
  55. $m->student_id = $s2->id;
  56. $m->save();
  57. $this->content = dump('Created school objects');
  58. $clubs = \Model\Club::fetch();
  59. foreach($clubs as $club)
  60. {
  61. $club->load();
  62. foreach($club->students() as $student)
  63. {
  64. /*
  65. * This student may have already been removed
  66. */
  67. if($student->load())
  68. {
  69. $this->content .= dump('Removing '. $student->name. ' and her records');
  70. // Remove their car, club membership, and them
  71. $student->delete();
  72. }
  73. }
  74. $club->delete();
  75. }
  76. foreach(\Model\Dorm::fetch() as $dorm)
  77. {
  78. $this->content .= dump('Removing the '. $dorm->name. ' dorm');
  79. $dorm->delete(); // Delete the dorm
  80. }
  81. $this->content .= dump('Removed school objects');
  82. // Load the view file
  83. $this->content .= new \Micro\View('School/Index');
  84. // Load global theme sidebar
  85. $this->sidebar = new \Micro\View('Sidebar');
  86. }
  87. }
  88. /* Table Schema:
  89. CREATE TABLE IF NOT EXISTS `car` (
  90. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  91. `name` varchar(100) DEFAULT NULL,
  92. `student_id` int(11) DEFAULT NULL,
  93. PRIMARY KEY (`id`),
  94. KEY `student_id` (`student_id`)
  95. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
  96. -- --------------------------------------------------------
  97. CREATE TABLE IF NOT EXISTS `club` (
  98. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  99. `name` varchar(100) DEFAULT NULL,
  100. PRIMARY KEY (`id`)
  101. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
  102. -- --------------------------------------------------------
  103. CREATE TABLE IF NOT EXISTS `dorm` (
  104. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  105. `name` varchar(100) DEFAULT NULL,
  106. PRIMARY KEY (`id`)
  107. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
  108. -- --------------------------------------------------------
  109. CREATE TABLE IF NOT EXISTS `membership` (
  110. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  111. `club_id` int(11) DEFAULT NULL,
  112. PRIMARY KEY (`id`),
  113. KEY `club_id` (`club_id`),
  114. KEY `student_id` (`student_id`)
  115. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
  116. -- --------------------------------------------------------
  117. CREATE TABLE IF NOT EXISTS `student` (
  118. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  119. `dorm_id` smallint(6) DEFAULT NULL,
  120. `name` varchar(100) DEFAULT NULL,
  121. PRIMARY KEY (`id`)
  122. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
  123. */