| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | class Doctrine_Inheritance_SingleTable_TestCase extends Doctrine_UnitTestCase |
|---|
| 5 | { |
|---|
| 6 | public function prepareData() |
|---|
| 7 | { } |
|---|
| 8 | |
|---|
| 9 | public function prepareTables() |
|---|
| 10 | { |
|---|
| 11 | $this->tables[] = 'STI_User'; |
|---|
| 12 | $this->tables[] = 'STI_Manager'; |
|---|
| 13 | $this->tables[] = 'STI_Customer'; |
|---|
| 14 | $this->tables[] = 'STI_SuperManager'; |
|---|
| 15 | parent::prepareTables(); |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | public function testMetadataSetup() |
|---|
| 19 | { |
|---|
| 20 | $userClass = $this->conn->getClassMetadata('STI_User'); |
|---|
| 21 | $superManagerClass = $this->conn->getClassMetadata('STI_SuperManager'); |
|---|
| 22 | $managerClass = $this->conn->getClassMetadata('STI_Manager'); |
|---|
| 23 | $customerClass = $this->conn->getClassMetadata('STI_Customer'); |
|---|
| 24 | |
|---|
| 25 | $this->assertEqual(4, count($userClass->getMappedColumns())); |
|---|
| 26 | $this->assertEqual('sti_entity', $userClass->getTableName()); |
|---|
| 27 | $this->assertEqual('sti_entity', $managerClass->getTableName()); |
|---|
| 28 | |
|---|
| 29 | // check inheritance map |
|---|
| 30 | $this->assertEqual(array(1 => 'STI_User', |
|---|
| 31 | 2 => 'STI_Manager', |
|---|
| 32 | 3 => 'STI_Customer', |
|---|
| 33 | 4 => 'STI_SuperManager'), $userClass->getInheritanceOption('discriminatorMap')); |
|---|
| 34 | |
|---|
| 35 | //var_dump($superManagerTable->getComponentName()); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | public function testSave() |
|---|
| 39 | { |
|---|
| 40 | $manager = new STI_Manager(); |
|---|
| 41 | $manager->salary = 80000; |
|---|
| 42 | $manager->name = 'John Smith'; |
|---|
| 43 | try { |
|---|
| 44 | $manager->save(); |
|---|
| 45 | $this->assertEqual(1, $manager->id); |
|---|
| 46 | $this->assertEqual(80000, $manager->salary); |
|---|
| 47 | $this->assertEqual('John Smith', $manager->name); |
|---|
| 48 | $this->assertEqual(2, $manager->type); |
|---|
| 49 | } catch (Exception $e) { |
|---|
| 50 | $this->fail("Saving record in single table inheritance failed: " . $e->getMessage()); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | public function testQuery() |
|---|
| 55 | { |
|---|
| 56 | //$this->_createManager(); |
|---|
| 57 | $query = $this->conn->createQuery(); |
|---|
| 58 | $query->select("m.*")->from("STI_Manager m"); |
|---|
| 59 | //echo $query->getSql(); |
|---|
| 60 | //$managers = $query->execute(); |
|---|
| 61 | |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | class STI_User extends Doctrine_Entity |
|---|
| 67 | { |
|---|
| 68 | public static function initMetadata($class) |
|---|
| 69 | { |
|---|
| 70 | $class->setInheritanceType(Doctrine::INHERITANCE_TYPE_SINGLE_TABLE, array( |
|---|
| 71 | 'discriminatorColumn' => 'type', |
|---|
| 72 | 'discriminatorMap' => array( |
|---|
| 73 | 1 => 'STI_User', |
|---|
| 74 | 2 => 'STI_Manager', |
|---|
| 75 | 3 => 'STI_Customer', |
|---|
| 76 | 4 => 'STI_SuperManager')) |
|---|
| 77 | ); |
|---|
| 78 | $class->setSubclasses(array('STI_Manager', 'STI_Customer', 'STI_SuperManager')); |
|---|
| 79 | $class->setTableName('sti_entity'); |
|---|
| 80 | $class->setColumn('sti_id as id', 'integer', 4, array('primary' => true, 'autoincrement' => true)); |
|---|
| 81 | $class->setColumn('sti_foo as foo', 'integer', 4); |
|---|
| 82 | $class->setColumn('sti_name as name', 'varchar', 50); |
|---|
| 83 | $class->setColumn('type', 'integer', 4); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | class STI_Manager extends STI_User |
|---|
| 88 | { |
|---|
| 89 | public static function initMetadata($class) |
|---|
| 90 | { |
|---|
| 91 | $class->setSubclasses(array('STI_SuperManager')); |
|---|
| 92 | $class->setColumn('stim_salary as salary', 'varchar', 50, array()); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | class STI_Customer extends STI_User |
|---|
| 97 | { |
|---|
| 98 | public static function initMetadata($class) |
|---|
| 99 | { |
|---|
| 100 | $class->setColumn('stic_bonuspoints as bonuspoints', 'varchar', 50, array()); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | class STI_SuperManager extends STI_Manager |
|---|
| 105 | { |
|---|
| 106 | public static function initMetadata($class) |
|---|
| 107 | { |
|---|
| 108 | $class->setColumn('stism_gosutitle as gosutitle', 'varchar', 50, array()); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|