Changeset 3961

Show
Ignore:
Timestamp:
03/10/08 06:41:15 (16 months ago)
Author:
jwage
Message:

fixes #796 fixes #797

Location:
branches/0.10
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • branches/0.10/lib/Doctrine/Import/Builder.php

    r3955 r3961  
    312312    public function buildTableDefinition(array $definition) 
    313313    { 
     314        if (isset($definition['inheritance']['type']) && $definition['inheritance']['type'] == 'simple') { 
     315            return; 
     316        } 
     317         
    314318        $ret = array(); 
    315319         
    316320        $i = 0; 
    317321         
    318         if (isset($definition['inheritance']['extends']) && ! (isset($definition['override_parent']) && $definition['override_parent'] == true)) { 
     322        if (isset($definition['inheritance']['type']) && $definition['inheritance']['type'] == 'concrete') { 
    319323            $ret[$i] = "    parent::setTableDefinition();"; 
    320324            $i++; 
     
    323327        if (isset($definition['tableName']) && !empty($definition['tableName'])) { 
    324328            $ret[$i] = "    ".'$this->setTableName(\''. $definition['tableName'].'\');'; 
    325              
    326329            $i++; 
    327330        } 
     
    344347        if (isset($definition['options']) && is_array($definition['options']) && !empty($definition['options'])) { 
    345348            $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']).');'; 
    346354            $i++; 
    347355        } 
     
    364372     */ 
    365373    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 
    367379        $ret = array(); 
    368380        $i = 0; 
    369381         
    370         if (isset($definition['inheritance']['extends']) && ! (isset($definition['override_parent']) && $definition['override_parent'] == true)) { 
     382        if (isset($definition['inheritance']['type']) && $definition['inheritance']['type'] == 'concrete') { 
    371383            $ret[$i] = "    parent::setUp();"; 
    372384            $i++; 
     
    428440                $i++; 
    429441            } 
    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'].'\'));'; 
    435442        } 
    436443 
  • branches/0.10/lib/Doctrine/Import/Schema.php

    r3914 r3961  
    130130 
    131131        $array = $this->_buildRelationships($array); 
     132        $array = $this->_processInheritance($array); 
    132133 
    133134        return $array; 
     
    225226            $className = isset($table['className']) ? (string) $table['className']:(string) $className; 
    226227 
     228            if (isset($table['inheritance']['keyField']) || isset($table['inheritance']['keyValue'])) { 
     229                $table['inheritance']['type'] = 'column_aggregation'; 
     230            } 
     231 
    227232            if (isset($table['tableName']) && $table['tableName']) { 
    228233                $tableName = $table['tableName']; 
    229234            } else { 
    230                 if (isset($table['inheritance']['extends'])) { 
     235                if (isset($table['inheritance']['type']) && ($table['inheritance']['type'] == 'column_aggregation')) { 
    231236                    $tableName = null; 
    232237                } else { 
     
    282287            // Apply the default values 
    283288            foreach ($defaults as $key => $defaultValue) { 
    284                 if (isset($table[$key]) && !isset($build[$className][$key])) { 
     289                if (isset($table[$key]) && ! isset($build[$className][$key])) { 
    285290                    $build[$className][$key] = $table[$key]; 
    286291                } else { 
     
    288293                } 
    289294            } 
    290  
     295             
    291296            $build[$className]['className'] = $className; 
    292297            $build[$className]['tableName'] = $tableName; 
    293298            $build[$className]['columns'] = $columns; 
    294  
     299             
    295300            // Make sure that anything else that is specified in the schema makes it to the final array 
    296301            $build[$className] = Doctrine_Lib::arrayDeepMerge($table, $build[$className]); 
     
    301306 
    302307        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; 
    303371    } 
    304372 
     
    317385        // User.contact_id will automatically create User hasOne Contact local => contact_id, foreign => id 
    318386        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']) { 
    320388                foreach ($properties['columns'] as $column) { 
    321389                    if (strpos($column['name'], '_id')) { 
  • branches/0.10/tests/Import/BuilderTestCase.php

    r3889 r3961  
    3535    public function testInheritanceGeneration() 
    3636    { 
    37         $path = realpath(dirname(__FILE__) . '/../..') . '/models/test_generated'; 
     37        $path = dirname(__FILE__) . '/import_builder_test'; 
    3838 
    3939        $import = new Doctrine_Import_Schema(); 
     
    7474        $this->assertTrue($schemaTestInheritanceChild2Table->isSubClassOf('PackageSchemaTestInheritanceChild1Table')); 
    7575 
     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 
    76113        Doctrine_Lib::removeDirectories($path); 
    77114    } 
  • branches/0.10/tests/Import/SchemaTestCase.php

    r3888 r3961  
    3838    public function testYmlImport() 
    3939    { 
    40         $path = realpath(dirname(__FILE__) . '/../..') . '/models/test_generated'; 
     40        $path = dirname(__FILE__) . '/import_builder_test'; 
    4141         
    4242        $import = new Doctrine_Import_Schema(); 
  • branches/0.10/tests/schema.yml

    r3888 r3961  
    8585    keyField: type 
    8686    keyValue: child2 
     87 
     88# Simple Inheritance 
     89# All children share the columns of the parent 
     90SchemaTestSimpleInheritanceParent: 
     91  columns: 
     92    name: string(255) 
     93 
     94SchemaTestSimpleInheritanceChild: 
     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 
     103SchemaTestClassTableInheritanceParent: 
     104  columns: 
     105    name: string(255) 
     106 
     107SchemaTestClassTableInheritanceChild: 
     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 
     117SchemaTestConcreteInheritanceParent: 
     118  columns: 
     119    name: string(255) 
     120 
     121SchemaTestConcreteInheritanceChild: 
     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 
     132SchemaTestColumnAggregationInheritanceParent: 
     133  columns: 
     134    name: string(255) 
     135    type: string(255) 
     136 
     137SchemaTestColumnAggregationInheritanceChild: 
     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)