Changeset 5328
- Timestamp:
- 12/31/08 20:49:04 (6 months ago)
- Location:
- branches
- Files:
-
- 2 modified
-
1.0/lib/Doctrine/Data/Import.php (modified) (11 diffs)
-
1.1/lib/Doctrine/Data/Import.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/1.0/lib/Doctrine/Data/Import.php
r5321 r5328 50 50 * Optionally pass the directory/path to the yaml for importing 51 51 * 52 * @param string $directory 52 * @param string $directory 53 53 * @return void 54 54 */ … … 106 106 { 107 107 $array = $this->doParsing(); 108 108 109 109 if ( ! $append) { 110 110 $this->purge(array_reverse(array_keys($array))); 111 111 } 112 112 113 113 $this->_loadData($array); 114 114 } … … 188 188 * Process a row and make all the appropriate relations between the imported data 189 189 * 190 * @param string $rowKey 191 * @param string $row 192 * @return void 193 */ 190 * @param string $rowKey 191 * @param string $row 192 * @return void 193 */ 194 194 protected function _processRow($rowKey, $row) 195 195 { … … 210 210 } else if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) { 211 211 $relation = $obj->$key; 212 212 213 213 $relation[] = $this->_getImportedObject($link, $obj, $key, $rowKey); 214 214 } … … 230 230 } 231 231 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 } 232 246 /** 233 247 * Perform the loading of the data from the passed array 234 248 * 235 * @param string $array 249 * @param string $array 236 250 * @return void 237 251 */ … … 248 262 } 249 263 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); 258 269 } else { 259 270 $this->_buildRows($className, $data); … … 274 285 } 275 286 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 276 294 $objects = array(); 277 295 foreach ($this->_importedObjects as $object) { … … 281 299 282 300 $manager = Doctrine_Manager::getInstance(); 283 foreach ($manager as $connection) { 301 foreach ($manager as $connection) { 284 302 $tree = $connection->unitOfWork->buildFlushTree($objects); 285 303 286 304 foreach ($tree as $model) { 287 305 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) { 291 308 $obj->save(); 292 309 } … … 295 312 } 296 313 297 foreach ($nestedSets as $className => $sets) {298 foreach ($sets as $data) {299 $this->_loadNestedSetData($className, $data);300 }301 }302 314 } 303 315 … … 305 317 * Load nested set data for models with nested set enabled 306 318 * 307 * @param string $model 308 * @param string $nestedSetData 309 * @param string $parent 319 * @param string $model 320 * @param string $nestedSetData 321 * @param string $parent 310 322 * @return void 311 323 */ … … 323 335 324 336 $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]); 325 339 326 340 if( ! $parent) { -
branches/1.1/lib/Doctrine/Data/Import.php
r5321 r5328 50 50 * Optionally pass the directory/path to the yaml for importing 51 51 * 52 * @param string $directory 52 * @param string $directory 53 53 * @return void 54 54 */ … … 106 106 { 107 107 $array = $this->doParsing(); 108 108 109 109 if ( ! $append) { 110 110 $this->purge(array_reverse(array_keys($array))); 111 111 } 112 112 113 113 $this->_loadData($array); 114 114 } … … 188 188 * Process a row and make all the appropriate relations between the imported data 189 189 * 190 * @param string $rowKey 191 * @param string $row 192 * @return void 193 */ 190 * @param string $rowKey 191 * @param string $row 192 * @return void 193 */ 194 194 protected function _processRow($rowKey, $row) 195 195 { … … 210 210 } else if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) { 211 211 $relation = $obj->$key; 212 212 213 213 $relation[] = $this->_getImportedObject($link, $obj, $key, $rowKey); 214 214 } … … 230 230 } 231 231 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 } 232 246 /** 233 247 * Perform the loading of the data from the passed array 234 248 * 235 * @param string $array 249 * @param string $array 236 250 * @return void 237 251 */ … … 248 262 } 249 263 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); 258 269 } else { 259 270 $this->_buildRows($className, $data); … … 274 285 } 275 286 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 276 294 $objects = array(); 277 295 foreach ($this->_importedObjects as $object) { … … 281 299 282 300 $manager = Doctrine_Manager::getInstance(); 283 foreach ($manager as $connection) { 301 foreach ($manager as $connection) { 284 302 $tree = $connection->unitOfWork->buildFlushTree($objects); 285 303 286 304 foreach ($tree as $model) { 287 305 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) { 291 308 $obj->save(); 292 309 } … … 295 312 } 296 313 297 foreach ($nestedSets as $className => $sets) {298 foreach ($sets as $data) {299 $this->_loadNestedSetData($className, $data);300 }301 }302 314 } 303 315 … … 305 317 * Load nested set data for models with nested set enabled 306 318 * 307 * @param string $model 308 * @param string $nestedSetData 309 * @param string $parent 319 * @param string $model 320 * @param string $nestedSetData 321 * @param string $parent 310 322 * @return void 311 323 */ … … 323 335 324 336 $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]); 325 339 326 340 if( ! $parent) {