1<?php 2 3/** 4 * Doctrine_Ticket_626_TestCase 5 * 6 * @package Doctrine 7 * @author Tamcy <7am.online@gmail.com> 8 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 9 * @category Object Relational Mapping 10 * @link www.doctrine-project.org 11 * @since 1.0 12 * @version $Revision$ 13 */ 14 15class Doctrine_Ticket_626B_TestCase extends Doctrine_UnitTestCase 16{ 17 public function prepareData() 18 { } 19 20 public function prepareTables() 21 { 22 $this->tables = array('T626_Group', 'T626B_Student', 'T626_Course', 'T626B_StudentCourse'); 23 parent::prepareTables(); 24 } 25 26 protected function newCourse($id, $name) 27 { 28 $course = new T626_Course(); 29 $course->id = $id; 30 $course->name = $name; 31 $course->save(); 32 return $course; 33 } 34 35 protected function newGroup($id, $name) 36 { 37 $group = new T626_Group(); 38 $group->id = $id; 39 $group->name = $name; 40 $group->save(); 41 return $group; 42 } 43 44 protected function newStudent($id, $name, $group) 45 { 46 $u = new T626B_Student(); 47 $u->id = $id; 48 $u->name = $name; 49 $u->group_id = $group->id; 50 $u->save(); 51 return $u; 52 } 53 54 protected function newStudentCourse($student, $course) 55 { 56 $sc = new T626B_StudentCourse; 57 $sc->student_id = $student->id; 58 $sc->course_id = $course->id; 59 $sc->save(); 60 return $sc; 61 } 62 63 public function testTicket() 64 { 65 $group1 = $this->newGroup('1', 'Group 1'); 66 $student1 = $this->newStudent('07090002', 'First Student', $group1); 67 $course1 = $this->newCourse('MATH001', 'Maths'); 68 $course2 = $this->newCourse('ENG002', 'English Literature'); 69 70 $this->newStudentCourse($student1, $course1); 71 $this->newStudentCourse($student1, $course2); 72 73 try { 74 $group = $student1->get('Group'); 75 $this->pass(); 76 } catch (Exception $e) { 77 $this->fail($e->__toString()); 78 } 79 80 try { 81 $courses = $student1->get('StudyCourses'); 82 $this->pass(); 83 } catch (Exception $e) { 84 $this->fail($e->__toString()); 85 } 86 87 } 88} 89 90 91class T626B_Student extends Doctrine_Record 92{ 93 public function setTableDefinition() 94 { 95 $this->setTableName('T626B_Student_record'); 96 97 $this->hasColumn('s_id as id', 'varchar', 30, array ( 'primary' => true,)); 98 $this->hasColumn('s_g_id as group_id', 'varchar', 30, array ('notnull'=>true)); 99 $this->hasColumn('s_name as name', 'varchar', 50, array ()); 100 } 101 102 public function setUp() 103 { 104 $this->hasMany('T626_Course as StudyCourses', array('refClass' => 'T626B_StudentCourse', 'local' => 'sc_student_id', 'foreign' => 'sc_course_id')); 105 $this->hasOne('T626_Group as Group', array('local' => 's_g_id', 'foreign' => 'g_id')); 106 } 107} 108 109class T626_Group extends Doctrine_Record 110{ 111 public function setTableDefinition() 112 { 113 $this->setTableName('T626B_Student_group'); 114 115 $this->hasColumn('g_id as id', 'varchar', 30, array ( 'primary' => true,)); 116 $this->hasColumn('g_name as name', 'varchar', 50, array ()); 117 } 118 119 public function setUp() 120 { 121 $this->hasMany('T626B_Student as Students', 122 array('local' => 'g_id', 'foreign' => 's_id')); 123 } 124} 125 126 127class T626_Course extends Doctrine_Record 128{ 129 public function setTableDefinition() 130 { 131 $this->setTableName('T626_course'); 132 133 $this->hasColumn('c_id as id', 'varchar', 20, array ( 'primary' => true,)); 134 $this->hasColumn('c_name as name', 'varchar', 50, array ()); 135 } 136 137 public function setUp() 138 { 139 $this->hasMany('T626B_Student as Students', array('refClass' => 'T626B_StudentCourse', 'local' => 'sc_course_id', 'foreign' => 'sc_student_id')); 140 } 141} 142 143class T626B_StudentCourse extends Doctrine_Record 144{ 145 public function setTableDefinition() 146 { 147 $this->setTableName('T626B_Student_course'); 148 149 $this->hasColumn('sc_student_id as student_id', 'varchar', 30, array ( 'primary' => true,)); 150 $this->hasColumn('sc_course_id as course_id', 'varchar', 20, array ( 'primary' => true,)); 151 $this->hasColumn('sc_remark as remark', 'varchar', 500, array ()); 152 } 153 154 public function setUp() 155 { 156 $this->hasOne('T626B_Student as Student', array('local' => 'sc_student_id', 'foreign' => 's_id')); 157 $this->hasOne('T626_Course as Course', array('local' => 'sc_course_id', 'foreign' => 'c_id')); 158 } 159} 160