Changeset 3961
- Timestamp:
- 03/10/08 06:41:15 (16 months ago)
- Location:
- branches/0.10
- Files:
-
- 5 modified
-
lib/Doctrine/Import/Builder.php (modified) (5 diffs)
-
lib/Doctrine/Import/Schema.php (modified) (6 diffs)
-
tests/Import/BuilderTestCase.php (modified) (2 diffs)
-
tests/Import/SchemaTestCase.php (modified) (1 diff)
-
tests/schema.yml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/0.10/lib/Doctrine/Import/Builder.php
r3955 r3961 312 312 public function buildTableDefinition(array $definition) 313 313 { 314 if (isset($definition['inheritance']['type']) && $definition['inheritance']['type'] == 'simple') { 315 return; 316 } 317 314 318 $ret = array(); 315 319 316 320 $i = 0; 317 321 318 if (isset($definition['inheritance'][' extends']) && ! (isset($definition['override_parent']) && $definition['override_parent'] == true)) {322 if (isset($definition['inheritance']['type']) && $definition['inheritance']['type'] == 'concrete') { 319 323 $ret[$i] = " parent::setTableDefinition();"; 320 324 $i++; … … 323 327 if (isset($definition['tableName']) && !empty($definition['tableName'])) { 324 328 $ret[$i] = " ".'$this->setTableName(\''. $definition['tableName'].'\');'; 325 326 329 $i++; 327 330 } … … 344 347 if (isset($definition['options']) && is_array($definition['options']) && !empty($definition['options'])) { 345 348 $ret[$i] = $this->buildOptions($definition['options']); 349 $i++; 350 } 351 352 if (isset($definition['inheritance']['subclasses']) && ! empty($definition['inheritance']['subclasses'])) { 353 $ret[$i] = " ".'$this->setSubClasses('. $this->varExport($definition['inheritance']['subclasses']).');'; 346 354 $i++; 347 355 } … … 364 372 */ 365 373 public function buildSetUp(array $definition) 366 { 374 { 375 if (isset($definition['inheritance']['type']) && ($definition['inheritance']['type'] == 'simple' || $definition['inheritance']['type'] == 'column_aggregation')) { 376 return; 377 } 378 367 379 $ret = array(); 368 380 $i = 0; 369 381 370 if (isset($definition['inheritance'][' extends']) && ! (isset($definition['override_parent']) && $definition['override_parent'] == true)) {382 if (isset($definition['inheritance']['type']) && $definition['inheritance']['type'] == 'concrete') { 371 383 $ret[$i] = " parent::setUp();"; 372 384 $i++; … … 428 440 $i++; 429 441 } 430 }431 432 if (isset($definition['inheritance']['keyField']) && isset($definition['inheritance']['keyValue'])) {433 $i++;434 $ret[$i] = " ".'$this->setInheritanceMap(array(\''.$definition['inheritance']['keyField'].'\' => \''.$definition['inheritance']['keyValue'].'\'));';435 442 } 436 443 -
branches/0.10/lib/Doctrine/Import/Schema.php
r3914 r3961 130 130 131 131 $array = $this->_buildRelationships($array); 132 $array = $this->_processInheritance($array); 132 133 133 134 return $array; … … 225 226 $className = isset($table['className']) ? (string) $table['className']:(string) $className; 226 227 228 if (isset($table['inheritance']['keyField']) || isset($table['inheritance']['keyValue'])) { 229 $table['inheritance']['type'] = 'column_aggregation'; 230 } 231 227 232 if (isset($table['tableName']) && $table['tableName']) { 228 233 $tableName = $table['tableName']; 229 234 } else { 230 if (isset($table['inheritance'][' extends'])) {235 if (isset($table['inheritance']['type']) && ($table['inheritance']['type'] == 'column_aggregation')) { 231 236 $tableName = null; 232 237 } else { … … 282 287 // Apply the default values 283 288 foreach ($defaults as $key => $defaultValue) { 284 if (isset($table[$key]) && ! isset($build[$className][$key])) {289 if (isset($table[$key]) && ! isset($build[$className][$key])) { 285 290 $build[$className][$key] = $table[$key]; 286 291 } else { … … 288 293 } 289 294 } 290 295 291 296 $build[$className]['className'] = $className; 292 297 $build[$className]['tableName'] = $tableName; 293 298 $build[$className]['columns'] = $columns; 294 299 295 300 // Make sure that anything else that is specified in the schema makes it to the final array 296 301 $build[$className] = Doctrine_Lib::arrayDeepMerge($table, $build[$className]); … … 301 306 302 307 return $build; 308 } 309 310 /** 311 * _processInheritance 312 * 313 * Perform some processing on inheritance. 314 * 315 * @param string $array 316 * @return void 317 */ 318 protected function _processInheritance($array) 319 { 320 // Apply default inheritance configuration 321 foreach ($array as $className => $definition) { 322 if ( ! empty($array[$className]['inheritance'])) { 323 // Default inheritance to concrete inheritance 324 if ( ! isset($array[$className]['inheritance']['type'])) { 325 $array[$className]['inheritance']['type'] = 'class_table'; 326 } 327 328 // Some magic for setting up the keyField and keyValue column aggregation options 329 // Adds keyField to the parent class automatically 330 if ($array[$className]['inheritance']['type'] == 'column_aggregation') { 331 // Set the keyField to 'type' by default 332 if ( ! isset($array[$className]['inheritance']['keyField'])) { 333 $array[$className]['inheritance']['keyField'] = 'type'; 334 } 335 336 // Set the keyValue to the name of the child class if it does not exist 337 if ( ! isset($array[$className]['inheritance']['keyValue'])) { 338 $array[$className]['inheritance']['keyValue'] = $className; 339 } 340 341 // Add the keyType column to the parent if a definition does not already exist 342 if ( ! isset($array[$array[$className]['inheritance']['extends']]['columns']['type'])) { 343 $array[$definition['inheritance']['extends']]['columns']['type'] = array('name' => 'type', 'type' => 'string', 'length' => 255); 344 } 345 } 346 } 347 } 348 349 // Array of the array keys to move to the parent, and the value to default the child definition to 350 // after moving it 351 $moves = array('columns' => array(), 'relations' => array()); 352 353 foreach ($array as $className => $definition) { 354 // Move any definitions on the schema to the parent 355 // Currently only columns and relations are moved. What else should we move? 356 if (isset($definition['inheritance']['extends']) && isset($definition['inheritance']['type']) && ($definition['inheritance']['type'] == 'simple' || $definition['inheritance']['type'] == 'column_aggregation')) { 357 $extends = $definition['inheritance']['extends']; 358 359 foreach ($moves as $move => $resetValue) { 360 $array[$extends][$move] = Doctrine_Lib::arrayDeepMerge($array[$extends][$move], $definition[$move]); 361 $array[$definition['className']][$move] = $resetValue; 362 } 363 364 if ($definition['inheritance']['type'] == 'column_aggregation') { 365 $array[$extends]['inheritance']['subclasses'][$definition['className']] = array($definition['inheritance']['keyField'] => $definition['inheritance']['keyValue']); 366 } 367 } 368 } 369 370 return $array; 303 371 } 304 372 … … 317 385 // User.contact_id will automatically create User hasOne Contact local => contact_id, foreign => id 318 386 foreach ($array as $className => $properties) { 319 if (isset($properties['columns']) && ! empty($properties['columns']) && isset($properties['detect_relations']) && $properties['detect_relations']) {387 if (isset($properties['columns']) && ! empty($properties['columns']) && isset($properties['detect_relations']) && $properties['detect_relations']) { 320 388 foreach ($properties['columns'] as $column) { 321 389 if (strpos($column['name'], '_id')) { -
branches/0.10/tests/Import/BuilderTestCase.php
r3889 r3961 35 35 public function testInheritanceGeneration() 36 36 { 37 $path = realpath(dirname(__FILE__) . '/../..') . '/models/test_generated';37 $path = dirname(__FILE__) . '/import_builder_test'; 38 38 39 39 $import = new Doctrine_Import_Schema(); … … 74 74 $this->assertTrue($schemaTestInheritanceChild2Table->isSubClassOf('PackageSchemaTestInheritanceChild1Table')); 75 75 76 # Simple Inheritance 77 $schemaTestSimpleInheritanceParent = new ReflectionClass('SchemaTestSimpleInheritanceParent'); 78 $schemaTestSimpleInheritanceChild = new ReflectionClass('SchemaTestSimpleInheritanceChild'); 79 80 $this->assertTrue($schemaTestSimpleInheritanceParent->hasMethod('setTableDefinition')); 81 $this->assertTrue($schemaTestSimpleInheritanceChild->isSubClassOf('SchemaTestSimpleInheritanceParent')); 82 83 # Class Table Inheritance 84 $schemaTestClassTableInheritanceParent = new ReflectionClass('SchemaTestClassTableInheritanceParent'); 85 $schemaTestClassTableInheritanceChild = new ReflectionClass('SchemaTestClassTableInheritanceChild'); 86 87 # Concrete Inheritance 88 $schemaTestConcreteInheritanceParent = new ReflectionClass('SchemaTestConcreteInheritanceParent'); 89 $schemaTestConcreteInheritanceChild = new ReflectionClass('SchemaTestConcreteInheritanceChild'); 90 91 # Column Aggregation Inheritance 92 $schemaTestColumnAggregationInheritanceParent = new ReflectionClass('SchemaTestColumnAggregationInheritanceParent'); 93 $schemaTestColumnAggregationInheritanceChild = new ReflectionClass('SchemaTestColumnAggregationInheritanceChild'); 94 95 $sql = Doctrine::generateSqlFromArray(array('SchemaTestSimpleInheritanceParent', 'SchemaTestSimpleInheritanceChild')); 96 $this->assertEqual(count($sql), 1); 97 $this->assertEqual($sql[0], 'CREATE TABLE schema_test_simple_inheritance_parent (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), description VARCHAR(255))'); 98 99 $sql = Doctrine::generateSqlFromArray(array('SchemaTestClassTableInheritanceParent', 'SchemaTestClassTableInheritanceChild')); 100 $this->assertEqual(count($sql), 2); 101 $this->assertEqual($sql[0], 'CREATE TABLE schema_test_class_table_inheritance_parent (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255))'); 102 $this->assertEqual($sql[1], 'CREATE TABLE schema_test_class_table_inheritance_child (id INTEGER, title VARCHAR(255), description VARCHAR(255), PRIMARY KEY(id))'); 103 104 $sql = Doctrine::generateSqlFromArray(array('SchemaTestConcreteInheritanceParent', 'SchemaTestConcreteInheritanceChild')); 105 $this->assertEqual(count($sql), 2); 106 $this->assertEqual($sql[0], 'CREATE TABLE schema_test_concrete_inheritance_parent (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255))'); 107 $this->assertEqual($sql[1], 'CREATE TABLE schema_test_concrete_inheritance_child (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), title VARCHAR(255), description VARCHAR(255))'); 108 109 $sql = Doctrine::generateSqlFromArray(array('SchemaTestColumnAggregationInheritanceParent', 'SchemaTestColumnAggregationInheritanceChild')); 110 $this->assertEqual(count($sql), 1); 111 $this->assertEqual($sql[0], 'CREATE TABLE schema_test_column_aggregation_inheritance_parent (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), type VARCHAR(255), title VARCHAR(255), description VARCHAR(255))'); 112 76 113 Doctrine_Lib::removeDirectories($path); 77 114 } -
branches/0.10/tests/Import/SchemaTestCase.php
r3888 r3961 38 38 public function testYmlImport() 39 39 { 40 $path = realpath(dirname(__FILE__) . '/../..') . '/models/test_generated';40 $path = dirname(__FILE__) . '/import_builder_test'; 41 41 42 42 $import = new Doctrine_Import_Schema(); -
branches/0.10/tests/schema.yml
r3888 r3961 85 85 keyField: type 86 86 keyValue: child2 87 88 # Simple Inheritance 89 # All children share the columns of the parent 90 SchemaTestSimpleInheritanceParent: 91 columns: 92 name: string(255) 93 94 SchemaTestSimpleInheritanceChild: 95 inheritance: 96 type: simple 97 extends: SchemaTestSimpleInheritanceParent 98 columns: 99 description: string(255) 100 101 # Class Table Inheritance 102 # Individual tables, each model has its own columns, and they share id primary keys 103 SchemaTestClassTableInheritanceParent: 104 columns: 105 name: string(255) 106 107 SchemaTestClassTableInheritanceChild: 108 inheritance: 109 type: class_table 110 extends: SchemaTestClassTableInheritanceParent 111 columns: 112 title: string(255) 113 description: string(255) 114 115 # Concrete Inheritance 116 # Individual tables, all children share the columns of its parents 117 SchemaTestConcreteInheritanceParent: 118 columns: 119 name: string(255) 120 121 SchemaTestConcreteInheritanceChild: 122 inheritance: 123 type: concrete 124 extends: SchemaTestClassTableInheritanceParent 125 columns: 126 title: string(255) 127 description: string(255) 128 129 # Column Aggregation Inheritance 130 # Single table, all children columns are stored in the parent, parent class has a type method 131 # Which is used to determine which child the record in the db belonds to 132 SchemaTestColumnAggregationInheritanceParent: 133 columns: 134 name: string(255) 135 type: string(255) 136 137 SchemaTestColumnAggregationInheritanceChild: 138 inheritance: 139 type: column_aggregation 140 keyField: type 141 keyValue: SchemaTestColumnAggregationInheritanceChild 142 extends: SchemaTestColumnAggregationInheritanceParent 143 columns: 144 title: string(255) 145 description: string(255)