Development

Changeset 7399

You must first sign up to be able to contribute.

Changeset 7399

Show
Ignore:
Timestamp:
02/08/08 08:09:40 (8 months ago)
Author:
fabien
Message:

added sfPropelData::getData() and changed propel:data-dump to dump data to standard output by default

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/sfPropelData.class.php

    r6973 r7399  
    283283   * Dumps data to fixture from one or more tables. 
    284284   * 
    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)); 
    296297    } 
    297298    else 
    298299    { 
    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  { 
    303324    $this->loadMapBuilders(); 
    304325    $this->con = Propel::getConnection($connectionName); 
     
    426447    } 
    427448 
    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; 
    446450  } 
    447451 
  • branches/1.1/lib/plugins/sfPropelPlugin/lib/task/sfPropelDataDumpTask.class.php

    r7397 r7399  
    2828    $this->addArguments(array( 
    2929      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'), 
    3131    )); 
    3232 
     
    4444The [propel:data-dump|INFO] task dumps database data: 
    4545 
    46   [./symfony propel:data-dump frontend dump|INFO] 
     46  [./symfony propel:data-dump frontend > data/fixtures/dump.yml|INFO] 
    4747 
    48 The task dumps the database data in [data/fixtures/%target%|COMMENT]. 
     48By default, the task outputs the data to the standard output, 
     49but you can also pass a filename as a second argument: 
    4950 
    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 
     53The task will dump data in [data/fixtures/%target%|COMMENT] 
     54(data/fixtures/dump.yml in the example). 
     55 
     56The dump file is in the YML format and can be re-imported by using 
    5157the [propel:data-load|INFO] task. 
    5258 
     
    7278    $databaseManager = new sfDatabaseManager(); 
    7379 
    74     if (!sfToolkit::isPathAbsolute($filename)) 
     80    if (!is_null($filename) && !sfToolkit::isPathAbsolute($filename)) 
    7581    { 
    7682      $dir = sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'fixtures'; 
    7783      $this->filesystem->mkdirs($dir); 
    7884      $filename = $dir.DIRECTORY_SEPARATOR.$filename; 
     85 
     86      $this->logSection('propel', sprintf('dumping data to "%s"', $filename)); 
    7987    } 
    8088 
    81     $this->log('propel', sprintf('dumping data to "%s"', $filename)); 
     89    $data = new sfPropelData(); 
    8290 
    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    } 
    8599  } 
    86100}