Permalink
Browse files

Added ORM school example.

  • Loading branch information...
1 parent ec74352 commit 4a9615bbc22a6917ed82d6dbb903df338550d227 @Xeoncross committed Nov 21, 2011
View
@@ -0,0 +1,153 @@
+<?php
+/**
+ * School
+ *
+ * Shows an example of a school system using the ORM
+ *
+ * @package MicroMVC
+ * @author David Pennington
+ * @copyright (c) 2011 MicroMVC Framework
+ * @license http://micromvc.com/license
+ ********************************** 80 Columns *********************************
+ */
+namespace Controller;
+
+class School extends \MyController
+{
+
+ public function run()
+ {
+ $this->load_database();
+
+ // You can over-ride this in certain models if needed,
+ // allowing you to use multiple databases.
+ // Model_Name::$db = new DB(config('other_database'));
+
+ // New Dorm
+ $d = new \Model\Dorm();
+ $d->name = 'Dorm 1';
+ $d->save();
+
+ // New Student in Dorm
+ $s1 = new \Model\Student();
+ $s1->name = 'Mary';
+ $s1->dorm_id = $d->id;
+ $s1->save();
+
+ // New Student in Dorm
+ $s2 = new \Model\Student();
+ $s2->name = 'Jane';
+ $s2->dorm_id = $d->id;
+ $s2->save();
+
+ // New Car for student
+ $c = new \Model\Car();
+ $c->name = 'Truck';
+ $c->student_id = $s1->id;
+ $c->save(); // Insert
+
+ $c->name = $s1->name. '\'s Truck'; // Change car name
+ $c->save(); // Update
+
+ // New Softball club
+ $c = new \Model\Club();
+ $c->name = 'Softball';
+ $c->save();
+
+ // Mary is in softball
+ $m = new \Model\Membership();
+ $m->club_id = $c->id;
+ $m->student_id = $s1->id;
+ $m->save();
+
+ // Jane is in softball
+ $m = new \Model\Membership();
+ $m->club_id = $c->id;
+ $m->student_id = $s2->id;
+ $m->save();
+
+ $this->content = dump('Created school objects');
+
+ $clubs = \Model\Club::fetch();
+ foreach($clubs as $club)
+ {
+ $club->load();
+ foreach($club->students() as $student)
+ {
+ /*
+ * This student may have already been removed
+ */
+ if($student->load())
+ {
+ $this->content .= dump('Removing '. $student->name. ' and her records');
+
+ // Remove their car, club membership, and them
+ $student->delete();
+ }
+ }
+ $club->delete();
+ }
+
+ foreach(\Model\Dorm::fetch() as $dorm)
+ {
+ $this->content .= dump('Removing the '. $dorm->name. ' dorm');
+ $dorm->delete(); // Delete the dorm
+ }
+
+ $this->content .= dump('Removed school objects');
+
+ // Load the view file
+ $this->content .= new \Core\View('School/Index');
+
+ // Load global theme sidebar
+ $this->sidebar = new \Core\View('Sidebar');
+
+ }
+}
+
+/* Table Schema:
+
+CREATE TABLE IF NOT EXISTS `car` (
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
+ `name` varchar(100) DEFAULT NULL,
+ `student_id` int(11) DEFAULT NULL,
+ PRIMARY KEY (`id`),
+ KEY `student_id` (`student_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
+
+-- --------------------------------------------------------
+
+CREATE TABLE IF NOT EXISTS `club` (
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
+ `name` varchar(100) DEFAULT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
+
+-- --------------------------------------------------------
+
+CREATE TABLE IF NOT EXISTS `dorm` (
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
+ `name` varchar(100) DEFAULT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
+
+-- --------------------------------------------------------
+
+CREATE TABLE IF NOT EXISTS `membership` (
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
+ `club_id` int(11) DEFAULT NULL,
+ PRIMARY KEY (`id`),
+ KEY `club_id` (`club_id`),
+ KEY `student_id` (`student_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
+
+-- --------------------------------------------------------
+
+CREATE TABLE IF NOT EXISTS `student` (
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
+ `dorm_id` smallint(6) DEFAULT NULL,
+ `name` varchar(100) DEFAULT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
+
+*/
View
@@ -341,15 +341,15 @@ public function __call($alias, $args)
throw new Exception ($alias . ' relation not found');
}
- $model = static::$has_many_through[$alias];
+ $array = static::$has_many_through[$alias];
- $foreign_key = key($model);
- $model = current($model);
+ $foreign_key = key($array);
+ $model = current($array);
- next($model);
+ next($array);
- $foreign_key_2 = key($model);
- $model_2 = current($model);
+ $foreign_key_2 = key($array);
+ $model_2 = current($array);
// Set the foreign key WHERE condition
$where = array($foreign_key => $this->key()) + $args[0];
View
@@ -0,0 +1,22 @@
+<?php
+/**
+ * Car Model
+ *
+ * @package MicroMVC
+ * @author David Pennington
+ * @copyright (c) 2011 MicroMVC Framework
+ * @license http://micromvc.com/license
+ ********************************** 80 Columns *********************************
+ */
+namespace Model;
+
+class Car extends \Core\ORM
+{
+ public static $table = 'car';
+ public static $foreign_key = 'car_id';
+
+ public static $belongs_to = array(
+ 'student' => '\Model\Student',
+ );
+
+}
View
@@ -0,0 +1,28 @@
+<?php
+/**
+ * Club Model
+ *
+ * @package MicroMVC
+ * @author David Pennington
+ * @copyright (c) 2011 MicroMVC Framework
+ * @license http://micromvc.com/license
+ ********************************** 80 Columns *********************************
+ */
+namespace Model;
+
+class Club extends \Core\ORM
+{
+ public static $table = 'club';
+ public static $foreign_key = 'club_id';
+
+ public static $has = array(
+ 'memberships' => '\Model\Membership'
+ );
+
+ public static $has_many_through = array(
+ 'students' => array(
+ 'club_id' => '\Model\Membership',
+ 'student_id' => '\Model\Student'
+ ),
+ );
+}
View
@@ -0,0 +1,22 @@
+<?php
+/**
+ * Dorm Model
+ *
+ * @package MicroMVC
+ * @author David Pennington
+ * @copyright (c) 2011 MicroMVC Framework
+ * @license http://micromvc.com/license
+ ********************************** 80 Columns *********************************
+ */
+namespace Model;
+
+class Dorm extends \Core\ORM
+{
+ public static $table = 'dorm';
+ public static $foreign_key = 'dorm_id';
+
+ public static $has = array(
+ 'students' => '\Model\Student',
+ );
+
+}
View
@@ -0,0 +1,22 @@
+<?php
+/**
+ * Membership Model
+ *
+ * @package MicroMVC
+ * @author David Pennington
+ * @copyright (c) 2011 MicroMVC Framework
+ * @license http://micromvc.com/license
+ ********************************** 80 Columns *********************************
+ */
+namespace Model;
+
+class Membership extends \Core\ORM
+{
+ public static $table = 'membership';
+ public static $foreign_key = 'membership_id';
+
+ public static $belongs_to = array(
+ 'student' => '\Model\Student',
+ 'club' => '\Model\Club',
+ );
+}
View
@@ -1,25 +0,0 @@
-<?php
-
-namespace Model;
-
-class Permission extends \Core\ORM
-{
- public static $table = 'permission';
- public static $foreign_key = 'permission_id';
-
- public static $belongs_to = array(
- 'role' => '\Model\Role',
- 'resource' => '\Model\Resource',
- );
-
- /**
- * Die scum!
- */
- public function purge()
- {
- $i = static::$db->i;
-
- static::$db->query('TRUNCATE TABLE '. $i. static::$table. $i);
- }
-
-}
View
@@ -1,22 +0,0 @@
-<?php
-
-namespace Model;
-
-class Resource extends \Core\ORM
-{
- public static $table = 'resource';
- public static $foreign_key = 'resource_id';
- public static $order_by = array('name' => 'desc');
- public static $cascade_delete = TRUE;
-
- public static $has = array(
- 'permissions' => '\Model\Permission',
- );
-
- public static $has_many_through = array(
- 'roles' => array(
- 'resource_id' => '\Model\Permission',
- 'role_id' => '\Model\Role'
- ),
- );
-}
View
@@ -1,25 +0,0 @@
-<?php
-namespace Model;
-
-class Role extends \Core\ORM
-{
- public static $table = 'role';
- public static $foreign_key = 'role_id';
- public static $cascade_delete = FALSE;
-
- public static $has = array(
- 'permissions' => '\Model\Permission',
- 'users' => '\Model\User',
- );
-
- public static $has_many_through = array(
- 'resources' => array('\Model\Permission' => '\Model\Resource'),
- );
-
- public function has_permission($resource_id)
- {
- $i = static::$db->i;
-
- return (bool) static::$db->column('SELECT * FROM '. $i. 'permission'. $i. ' WHERE role_id = ? AND resource_id = ?', array($this->id, $resource_id));
- }
-}
View
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Student Model
+ *
+ * @package MicroMVC
+ * @author David Pennington
+ * @copyright (c) 2011 MicroMVC Framework
+ * @license http://micromvc.com/license
+ ********************************** 80 Columns *********************************
+ */
+namespace Model;
+
+class Student extends \Core\ORM
+{
+ public static $table = 'student';
+ public static $foreign_key = 'student_id';
+ public static $cascade_delete = TRUE;
+
+ public static $has = array(
+ 'car' => '\Model\Car',
+ 'memberships' => '\Model\Membership'
+ );
+
+ public static $belongs_to = array(
+ 'dorm' => '\Model\Dorm',
+ );
+
+ public static $has_many_through = array(
+ 'clubs' => array(
+ 'student_id' => '\Model\Membership',
+ 'club_id' => '\Model\Club'
+ ),
+ );
+}
View
@@ -1,14 +0,0 @@
-<?php
-namespace Model;
-
-class User extends \Core\ORM
-{
- public static $table = 'user';
- public static $foreign_key = 'user_id';
-
- public static function byName($name)
- {
- $name = static::$db->quote("%$name%");
- return self::row(array("name LIKE $name"));
- }
-}
Oops, something went wrong.

0 comments on commit 4a9615b

Please sign in to comment.