Changeset 7399
- Timestamp:
- 02/08/08 08:09:40 (8 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/sfPropelData.class.php
r6973 r7399 283 283 * Dumps data to fixture from one or more tables. 284 284 * 285 * @param string directory or file to dump to 286 * @param mixed name or names of tables to dump (or all to dump all tables) 287 * @param string connection name 288 */ 289 public function dumpData($directory_or_file = null, $tables = 'all', $connectionName = 'propel') 290 { 291 $sameFile = true; 292 if (is_dir($directory_or_file)) 293 { 294 // multi files 295 $sameFile = false; 285 * @param string The directory or file to dump to 286 * @param mixed The name or names of tables to dump (or all to dump all tables) 287 * @param string The connection name (default to propel) 288 */ 289 public function dumpData($directory_or_file, $tables = 'all', $connectionName = 'propel') 290 { 291 $dumpData = $this->getData($tables, $connectionName); 292 293 // save to file(s) 294 if (!is_dir($directory_or_file)) 295 { 296 file_put_contents($directory_or_file, sfYaml::dump($dumpData)); 296 297 } 297 298 else 298 299 { 299 // same file 300 // delete file 301 } 302 300 $i = 0; 301 foreach ($tables as $tableName) 302 { 303 if (!isset($dumpData[$tableName])) 304 { 305 continue; 306 } 307 308 file_put_contents(sprintf("%s/%03d-%s.yml", $directory_or_file, ++$i, $tableName), sfYaml::dump(array($tableName => $dumpData[$tableName]))); 309 } 310 } 311 } 312 313 /** 314 * Returns data from one or more tables. 315 * 316 * @param string directory or file to dump to 317 * @param mixed name or names of tables to dump (or all to dump all tables) 318 * @param string connection name 319 * 320 * @return array An array of database data 321 */ 322 public function getData($tables = 'all', $connectionName = 'propel') 323 { 303 324 $this->loadMapBuilders(); 304 325 $this->con = Propel::getConnection($connectionName); … … 426 447 } 427 448 428 // save to file(s) 429 if ($sameFile) 430 { 431 file_put_contents($directory_or_file, Spyc::YAMLDump($dumpData)); 432 } 433 else 434 { 435 $i = 0; 436 foreach ($tables as $tableName) 437 { 438 if (!isset($dumpData[$tableName])) 439 { 440 continue; 441 } 442 443 file_put_contents(sprintf("%s/%03d-%s.yml", $directory_or_file, ++$i, $tableName), Spyc::YAMLDump(array($tableName => $dumpData[$tableName]))); 444 } 445 } 449 return $dumpData; 446 450 } 447 451 branches/1.1/lib/plugins/sfPropelPlugin/lib/task/sfPropelDataDumpTask.class.php
r7397 r7399 28 28 $this->addArguments(array( 29 29 new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'), 30 new sfCommandArgument('target', sfCommandArgument:: REQUIRED, 'The target filename'),30 new sfCommandArgument('target', sfCommandArgument::OPTIONAL, 'The target filename'), 31 31 )); 32 32 … … 44 44 The [propel:data-dump|INFO] task dumps database data: 45 45 46 [./symfony propel:data-dump frontend dump|INFO]46 [./symfony propel:data-dump frontend > data/fixtures/dump.yml|INFO] 47 47 48 The task dumps the database data in [data/fixtures/%target%|COMMENT]. 48 By default, the task outputs the data to the standard output, 49 but you can also pass a filename as a second argument: 49 50 50 The dump file is in the YML format and can be reimported by using 51 [./symfony propel:data-dump frontend dump.yml|INFO] 52 53 The task will dump data in [data/fixtures/%target%|COMMENT] 54 (data/fixtures/dump.yml in the example). 55 56 The dump file is in the YML format and can be re-imported by using 51 57 the [propel:data-load|INFO] task. 52 58 … … 72 78 $databaseManager = new sfDatabaseManager(); 73 79 74 if (! sfToolkit::isPathAbsolute($filename))80 if (!is_null($filename) && !sfToolkit::isPathAbsolute($filename)) 75 81 { 76 82 $dir = sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'fixtures'; 77 83 $this->filesystem->mkdirs($dir); 78 84 $filename = $dir.DIRECTORY_SEPARATOR.$filename; 85 86 $this->logSection('propel', sprintf('dumping data to "%s"', $filename)); 79 87 } 80 88 81 $ this->log('propel', sprintf('dumping data to "%s"', $filename));89 $data = new sfPropelData(); 82 90 83 $data = new sfPropelData(); 84 $data->dumpData($filename, 'all', $options['connection']); 91 if (!is_null($filename)) 92 { 93 $data->dumpData($filename, 'all', $options['connection']); 94 } 95 else 96 { 97 fwrite(STDOUT, sfYaml::dump($data->getData('all', $options['connection']))); 98 } 85 99 } 86 100 }