Development

Changeset 6019

You must first sign up to be able to contribute.

Changeset 6019

Show
Ignore:
Timestamp:
11/14/07 12:28:24 (2 years ago)
Author:
fabien
Message:

fixed sfPropel::Data::dumpData() (patch from fred)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.0/lib/addon/propel/sfPropelData.class.php

    r5843 r6019  
    111111        foreach ($data as $name => $value) 
    112112        { 
     113          $isARealColumn = true; 
     114          try 
     115          { 
     116            $column = $tableMap->getColumn($name); 
     117          } 
     118          catch (PropelException $e) 
     119          { 
     120            $isARealColumn = false; 
     121          } 
     122 
    113123          // foreign key? 
    114           try 
    115           { 
    116             $column = $tableMap->getColumn($name); 
     124          if ($isARealColumn) 
     125          { 
    117126            if ($column->isForeignKey() && !is_null($value)) 
    118127            { 
    119128              $relatedTable = $this->maps[$class]->getDatabaseMap()->getTable($column->getRelatedTableName()); 
     129 
    120130              if (!isset($this->object_references[$relatedTable->getPhpName().'_'.$value])) 
    121131              { 
    122                 $error = 'The object "%s" from class "%s" is not defined in your data file.'; 
    123                 $error = sprintf($error, $value, $relatedTable->getPhpName()); 
    124                 throw new sfException($error); 
     132                throw new sfException(sprintf('The object "%s" from class "%s" is not defined in your data file.', $value, $relatedTable->getPhpName())); 
    125133              } 
     134 
    126135              $value = $this->object_references[$relatedTable->getPhpName().'_'.$value]; 
    127136            } 
    128           } 
    129           catch (PropelException $e) 
    130           { 
    131137          } 
    132138 
     
    287293 
    288294    $tables = $this->fixOrderingOfForeignKeyData($tables); 
    289  
    290295    foreach ($tables as $tableName) 
    291296    { 
    292297      $tableMap = $this->maps[$tableName]->getDatabaseMap()->getTable(constant($tableName.'Peer::TABLE_NAME')); 
     298      $hasParent = false; 
     299      $haveParents = false; 
     300      $fixColumn = null; 
     301      foreach ($tableMap->getColumns() as $column) 
     302      { 
     303        $col = strtolower($column->getColumnName()); 
     304        if ($column->isForeignKey()) 
     305        { 
     306          $relatedTable = $this->maps[$tableName]->getDatabaseMap()->getTable($column->getRelatedTableName()); 
     307          if ($tableName === $relatedTable->getPhpName()) 
     308          { 
     309            if ($hasParent) 
     310            { 
     311              $haveParents = true; 
     312            } 
     313            else 
     314            { 
     315              $fixColumn = $column; 
     316              $hasParent = true; 
     317            } 
     318          } 
     319        } 
     320      } 
     321 
     322      if ($haveParents) 
     323      { 
     324        // unable to dump tables having multi-recursive references 
     325        continue; 
     326      } 
    293327 
    294328      // get db info 
    295       $rs = $this->con->executeQuery('SELECT * FROM '.constant($tableName.'Peer::TABLE_NAME')); 
    296  
    297       while ($rs->next()) 
    298       { 
    299         $pk = $tableName; 
    300         $values = array(); 
    301         foreach ($tableMap->getColumns() as $column) 
    302         { 
    303           $col = strtolower($column->getColumnName()); 
    304           if ($column->isPrimaryKey()) 
    305           { 
    306             $pk .= '_'.$rs->get($col); 
    307           } 
    308           else if ($column->isForeignKey()) 
    309           { 
    310             $relatedTable = $this->maps[$tableName]->getDatabaseMap()->getTable($column->getRelatedTableName()); 
    311  
    312             $values[$col] = $relatedTable->getPhpName().'_'.$rs->get($col); 
    313           } 
    314           else 
    315           { 
    316             $values[$col] = $rs->get($col); 
    317           } 
    318         } 
    319  
    320         if (!isset($dumpData[$tableName])) 
    321         { 
     329      $resultsSets = array(); 
     330      if ($hasParent) 
     331      { 
     332        $resultsSets = $this->fixOrderingOfForeignKeyDataInSameTable($resultsSets, $tableName, $fixColumn); 
     333      } 
     334      else 
     335      { 
     336        $resultsSets[] = $this->con->executeQuery('SELECT * FROM '.constant($tableName.'Peer::TABLE_NAME')); 
     337      } 
     338 
     339      foreach ($resultsSets as $rs) 
     340      { 
     341        if($rs->getRecordCount() > 0 && !isset($dumpData[$tableName])){ 
    322342          $dumpData[$tableName] = array(); 
    323343        } 
    324344 
    325         $dumpData[$tableName][$pk] = $values; 
     345        while ($rs->next()) 
     346        { 
     347          $pk = $tableName; 
     348          $values = array(); 
     349          $primaryKeys = array(); 
     350          $foreignKeys = array(); 
     351 
     352          foreach ($tableMap->getColumns() as $column) 
     353          { 
     354            $col = strtolower($column->getColumnName()); 
     355            $isPrimaryKey = $column->isPrimaryKey(); 
     356 
     357            if (is_null($rs->get($col))) 
     358            { 
     359              continue; 
     360            } 
     361 
     362            if ($isPrimaryKey) 
     363            { 
     364              $value = $rs->get($col); 
     365              $pk .= '_'.$value; 
     366              $primaryKeys[$col] = $value; 
     367            } 
     368 
     369            if ($column->isForeignKey()) 
     370            { 
     371              $relatedTable = $this->maps[$tableName]->getDatabaseMap()->getTable($column->getRelatedTableName()); 
     372              if ($isPrimaryKey) 
     373              { 
     374                $foreignKeys[$col] = $rs->get($col); 
     375                $primaryKeys[$col] = $relatedTable->getPhpName().'_'.$rs->get($col); 
     376              } 
     377              else 
     378              { 
     379                $values[$col] = $relatedTable->getPhpName().'_'.$rs->get($col); 
     380              } 
     381            } 
     382            elseif (!$isPrimaryKey || ($isPrimaryKey && !$tableMap->isUseIdGenerator())) 
     383            { 
     384              // We did not want auto incremented primary keys 
     385              $values[$col] = $rs->get($col); 
     386            } 
     387          } 
     388 
     389          if (count($primaryKeys) > 1 || (count($primaryKeys) > 0 && count($foreignKeys) > 0)) 
     390          { 
     391            $values = array_merge($primaryKeys, $values); 
     392          } 
     393 
     394          $dumpData[$tableName][$pk] = $values; 
     395        } 
    326396      } 
    327397    } 
     
    386456    return $classes; 
    387457  } 
     458   
     459  protected function fixOrderingOfForeignKeyDataInSameTable($resultsSets, $tableName, $column, $in = null) 
     460  { 
     461    $rs = $this->con->executeQuery(sprintf('SELECT * FROM %s WHERE %s %s', 
     462      constant($tableName.'Peer::TABLE_NAME'), 
     463      strtolower($column->getColumnName()), 
     464      is_null($in) ? 'IS NULL' : 'IN ('.$in.')' 
     465    )); 
     466    $in = array(); 
     467    while ($rs->next()) 
     468    { 
     469      $in[] = "'".$rs->get(strtolower($column->getRelatedColumnName()))."'"; 
     470    } 
     471 
     472    if ($in = implode(', ', $in)) 
     473    { 
     474      $rs->seek(0); 
     475      $resultsSets[] = $rs; 
     476      $resultsSets = $this->fixOrderingOfForeignKeyDataInSameTable($resultsSets, $tableName, $column, $in); 
     477    } 
     478 
     479    return $resultsSets; 
     480  } 
    388481} 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.