Changeset 5328

Show
Ignore:
Timestamp:
12/31/08 20:49:04 (6 months ago)
Author:
guilhermeblanco
Message:

[1.0, 1.1] fizes #1701. Fixed data fixture importing of NestedSet? model. Thanks for the patch!

Location:
branches
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/1.0/lib/Doctrine/Data/Import.php

    r5321 r5328  
    5050     * Optionally pass the directory/path to the yaml for importing 
    5151     * 
    52      * @param string $directory  
     52     * @param string $directory 
    5353     * @return void 
    5454     */ 
     
    106106    { 
    107107        $array = $this->doParsing(); 
    108          
     108 
    109109        if ( ! $append) { 
    110110            $this->purge(array_reverse(array_keys($array))); 
    111111        } 
    112          
     112 
    113113        $this->_loadData($array); 
    114114    } 
     
    188188     * Process a row and make all the appropriate relations between the imported data 
    189189     * 
    190      * @param string $rowKey  
    191      * @param string $row  
    192      * @return void 
    193      */     
     190     * @param string $rowKey 
     191     * @param string $row 
     192     * @return void 
     193     */ 
    194194    protected function _processRow($rowKey, $row) 
    195195    { 
     
    210210                            } else if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) { 
    211211                                $relation = $obj->$key; 
    212                                  
     212 
    213213                                $relation[] = $this->_getImportedObject($link, $obj, $key, $rowKey); 
    214214                            } 
     
    230230    } 
    231231 
     232   /** 
     233    * NestedSet fixtures may come in a 'natural' format with nested children listed under a 'children' 
     234    * key or in a raw, non-nested format with lft/rgt values. 
     235    * 
     236    * This method returns true if the given $data is a nested set in 'natural' form. 
     237    * 
     238    * @param $className 
     239    * @param $data 
     240    * @return boolean 
     241    */ 
     242    protected function _hasNaturalNestedSetFormat($className, array $data) { 
     243                $first = current($data); 
     244                return isset($first['children']) && Doctrine::getTable($className)->isTree(); 
     245    } 
    232246    /** 
    233247     * Perform the loading of the data from the passed array 
    234248     * 
    235      * @param string $array  
     249     * @param string $array 
    236250     * @return void 
    237251     */ 
     
    248262            } 
    249263 
    250             if (Doctrine::getTable($className)->isTree()) { 
    251                 $first = current($data); 
    252                 if (isset($first['children'])) { 
    253                     $nestedSets[$className][] = $data; 
    254                     $this->_buildNestedSetRows($className, $data); 
    255                 } else { 
    256                     $this->_buildRows($className, $data); 
    257                 } 
     264            // if loaded data is a nested set in natural format, process through _buildNestedSetRows. 
     265            // 'raw' nested sets and all other models are processed through _buildRows. 
     266            if ($this->_hasNaturalNestedSetFormat($className, $data)) { 
     267                $nestedSets[$className][] = $data; 
     268                $this->_buildNestedSetRows($className, $data); 
    258269            } else { 
    259270                $this->_buildRows($className, $data); 
     
    274285        } 
    275286 
     287        // save natural nested set fixture data and unset from _importedObjects 
     288        foreach ($nestedSets as $className => $sets) { 
     289            foreach ($sets as $data) { 
     290                $this->_loadNestedSetData($className, $data); 
     291            } 
     292        } 
     293 
    276294        $objects = array(); 
    277295        foreach ($this->_importedObjects as $object) { 
     
    281299 
    282300        $manager = Doctrine_Manager::getInstance(); 
    283         foreach ($manager as $connection) {             
     301        foreach ($manager as $connection) { 
    284302            $tree = $connection->unitOfWork->buildFlushTree($objects); 
    285303 
    286304            foreach ($tree as $model) { 
    287305                foreach ($this->_importedObjects as $obj) { 
    288                     $templates = array_keys($obj->getTable()->getTemplates()); 
    289                      
    290                     if ($obj instanceof $model && ! $obj->getTable()->isTree()) { 
     306 
     307                    if ($obj instanceof $model) { 
    291308                        $obj->save(); 
    292309                    } 
     
    295312        } 
    296313 
    297         foreach ($nestedSets as $className => $sets) { 
    298             foreach ($sets as $data) { 
    299                 $this->_loadNestedSetData($className, $data); 
    300             } 
    301         } 
    302314    } 
    303315 
     
    305317     * Load nested set data for models with nested set enabled 
    306318     * 
    307      * @param string $model  
    308      * @param string $nestedSetData  
    309      * @param string $parent  
     319     * @param string $model 
     320     * @param string $nestedSetData 
     321     * @param string $parent 
    310322     * @return void 
    311323     */ 
     
    323335 
    324336            $record = $this->_importedObjects[$rowKey]; 
     337            // remove this nested set from _importedObjects so it's not processed in the save routine for normal objects 
     338            unset($this->_importedObjects[$rowKey]); 
    325339 
    326340            if( ! $parent) { 
  • branches/1.1/lib/Doctrine/Data/Import.php

    r5321 r5328  
    5050     * Optionally pass the directory/path to the yaml for importing 
    5151     * 
    52      * @param string $directory  
     52     * @param string $directory 
    5353     * @return void 
    5454     */ 
     
    106106    { 
    107107        $array = $this->doParsing(); 
    108          
     108 
    109109        if ( ! $append) { 
    110110            $this->purge(array_reverse(array_keys($array))); 
    111111        } 
    112          
     112 
    113113        $this->_loadData($array); 
    114114    } 
     
    188188     * Process a row and make all the appropriate relations between the imported data 
    189189     * 
    190      * @param string $rowKey  
    191      * @param string $row  
    192      * @return void 
    193      */     
     190     * @param string $rowKey 
     191     * @param string $row 
     192     * @return void 
     193     */ 
    194194    protected function _processRow($rowKey, $row) 
    195195    { 
     
    210210                            } else if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) { 
    211211                                $relation = $obj->$key; 
    212                                  
     212 
    213213                                $relation[] = $this->_getImportedObject($link, $obj, $key, $rowKey); 
    214214                            } 
     
    230230    } 
    231231 
     232   /** 
     233    * NestedSet fixtures may come in a 'natural' format with nested children listed under a 'children' 
     234    * key or in a raw, non-nested format with lft/rgt values. 
     235    * 
     236    * This method returns true if the given $data is a nested set in 'natural' form. 
     237    * 
     238    * @param $className 
     239    * @param $data 
     240    * @return boolean 
     241    */ 
     242    protected function _hasNaturalNestedSetFormat($className, array $data) { 
     243                $first = current($data); 
     244                return isset($first['children']) && Doctrine::getTable($className)->isTree(); 
     245    } 
    232246    /** 
    233247     * Perform the loading of the data from the passed array 
    234248     * 
    235      * @param string $array  
     249     * @param string $array 
    236250     * @return void 
    237251     */ 
     
    248262            } 
    249263 
    250             if (Doctrine::getTable($className)->isTree()) { 
    251                 $first = current($data); 
    252                 if (isset($first['children'])) { 
    253                     $nestedSets[$className][] = $data; 
    254                     $this->_buildNestedSetRows($className, $data); 
    255                 } else { 
    256                     $this->_buildRows($className, $data); 
    257                 } 
     264            // if loaded data is a nested set in natural format, process through _buildNestedSetRows. 
     265            // 'raw' nested sets and all other models are processed through _buildRows. 
     266            if ($this->_hasNaturalNestedSetFormat($className, $data)) { 
     267                $nestedSets[$className][] = $data; 
     268                $this->_buildNestedSetRows($className, $data); 
    258269            } else { 
    259270                $this->_buildRows($className, $data); 
     
    274285        } 
    275286 
     287        // save natural nested set fixture data and unset from _importedObjects 
     288        foreach ($nestedSets as $className => $sets) { 
     289            foreach ($sets as $data) { 
     290                $this->_loadNestedSetData($className, $data); 
     291            } 
     292        } 
     293 
    276294        $objects = array(); 
    277295        foreach ($this->_importedObjects as $object) { 
     
    281299 
    282300        $manager = Doctrine_Manager::getInstance(); 
    283         foreach ($manager as $connection) {             
     301        foreach ($manager as $connection) { 
    284302            $tree = $connection->unitOfWork->buildFlushTree($objects); 
    285303 
    286304            foreach ($tree as $model) { 
    287305                foreach ($this->_importedObjects as $obj) { 
    288                     $templates = array_keys($obj->getTable()->getTemplates()); 
    289                      
    290                     if ($obj instanceof $model && ! $obj->getTable()->isTree()) { 
     306 
     307                    if ($obj instanceof $model) { 
    291308                        $obj->save(); 
    292309                    } 
     
    295312        } 
    296313 
    297         foreach ($nestedSets as $className => $sets) { 
    298             foreach ($sets as $data) { 
    299                 $this->_loadNestedSetData($className, $data); 
    300             } 
    301         } 
    302314    } 
    303315 
     
    305317     * Load nested set data for models with nested set enabled 
    306318     * 
    307      * @param string $model  
    308      * @param string $nestedSetData  
    309      * @param string $parent  
     319     * @param string $model 
     320     * @param string $nestedSetData 
     321     * @param string $parent 
    310322     * @return void 
    311323     */ 
     
    323335 
    324336            $record = $this->_importedObjects[$rowKey]; 
     337            // remove this nested set from _importedObjects so it's not processed in the save routine for normal objects 
     338            unset($this->_importedObjects[$rowKey]); 
    325339 
    326340            if( ! $parent) {