Development

Changeset 6020

You must first sign up to be able to contribute.

Changeset 6020

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

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

Files:

Legend:

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

    r5839 r6020  
    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            { 
     
    125134            } 
    126135          } 
    127           catch (PropelException $e) 
    128           { 
    129           } 
    130136 
    131137          if (false !== $pos = array_search($name, $column_names)) 
     
    283289 
    284290    $tables = $this->fixOrderingOfForeignKeyData($tables); 
    285  
    286291    foreach ($tables as $tableName) 
    287292    { 
    288293      $tableMap = $this->maps[$tableName]->getDatabaseMap()->getTable(constant($tableName.'Peer::TABLE_NAME')); 
     294      $hasParent = false; 
     295      $haveParents = false; 
     296      $fixColumn = null; 
     297      foreach ($tableMap->getColumns() as $column) 
     298      { 
     299        $col = strtolower($column->getColumnName()); 
     300        if ($column->isForeignKey()) 
     301        { 
     302          $relatedTable = $this->maps[$tableName]->getDatabaseMap()->getTable($column->getRelatedTableName()); 
     303          if ($tableName === $relatedTable->getPhpName()) 
     304          { 
     305            if ($hasParent) 
     306            { 
     307              $haveParents = true; 
     308            } 
     309            else 
     310            { 
     311              $fixColumn = $column; 
     312              $hasParent = true; 
     313            } 
     314          } 
     315        } 
     316      } 
     317 
     318      if ($haveParents) 
     319      { 
     320        // unable to dump tables having multi-recursive references 
     321        continue; 
     322      } 
    289323 
    290324      // get db info 
    291       $rs = $this->con->executeQuery('SELECT * FROM '.constant($tableName.'Peer::TABLE_NAME')); 
    292  
    293       while ($rs->next()) 
    294       { 
    295         $pk = $tableName; 
    296         $values = array(); 
    297         foreach ($tableMap->getColumns() as $column) 
    298         { 
    299           $col = strtolower($column->getColumnName()); 
    300           if ($column->isPrimaryKey()) 
    301           { 
    302             $pk .= '_'.$rs->get($col); 
    303           } 
    304           else if ($column->isForeignKey()) 
    305           { 
    306             $relatedTable = $this->maps[$tableName]->getDatabaseMap()->getTable($column->getRelatedTableName()); 
    307  
    308             $values[$col] = $relatedTable->getPhpName().'_'.$rs->get($col); 
    309           } 
    310           else 
    311           { 
    312             $values[$col] = $rs->get($col); 
    313           } 
    314         } 
    315  
    316         if (!isset($dumpData[$tableName])) 
    317         { 
     325      $resultsSets = array(); 
     326      if ($hasParent) 
     327      { 
     328        $resultsSets = $this->fixOrderingOfForeignKeyDataInSameTable($resultsSets, $tableName, $fixColumn); 
     329      } 
     330      else 
     331      { 
     332        $resultsSets[] = $this->con->executeQuery('SELECT * FROM '.constant($tableName.'Peer::TABLE_NAME')); 
     333      } 
     334 
     335      foreach ($resultsSets as $rs) 
     336      { 
     337        if($rs->getRecordCount() > 0 && !isset($dumpData[$tableName])){ 
    318338          $dumpData[$tableName] = array(); 
    319339        } 
    320340 
    321         $dumpData[$tableName][$pk] = $values; 
     341        while ($rs->next()) 
     342        { 
     343          $pk = $tableName; 
     344          $values = array(); 
     345          $primaryKeys = array(); 
     346          $foreignKeys = array(); 
     347 
     348          foreach ($tableMap->getColumns() as $column) 
     349          { 
     350            $col = strtolower($column->getColumnName()); 
     351            $isPrimaryKey = $column->isPrimaryKey(); 
     352 
     353            if (is_null($rs->get($col))) 
     354            { 
     355              continue; 
     356            } 
     357 
     358            if ($isPrimaryKey) 
     359            { 
     360              $value = $rs->get($col); 
     361              $pk .= '_'.$value; 
     362              $primaryKeys[$col] = $value; 
     363            } 
     364 
     365            if ($column->isForeignKey()) 
     366            { 
     367              $relatedTable = $this->maps[$tableName]->getDatabaseMap()->getTable($column->getRelatedTableName()); 
     368              if ($isPrimaryKey) 
     369              { 
     370                $foreignKeys[$col] = $rs->get($col); 
     371                $primaryKeys[$col] = $relatedTable->getPhpName().'_'.$rs->get($col); 
     372              } 
     373              else 
     374              { 
     375                $values[$col] = $relatedTable->getPhpName().'_'.$rs->get($col); 
     376              } 
     377            } 
     378            elseif (!$isPrimaryKey || ($isPrimaryKey && !$tableMap->isUseIdGenerator())) 
     379            { 
     380              // We did not want auto incremented primary keys 
     381              $values[$col] = $rs->get($col); 
     382            } 
     383          } 
     384 
     385          if (count($primaryKeys) > 1 || (count($primaryKeys) > 0 && count($foreignKeys) > 0)) 
     386          { 
     387            $values = array_merge($primaryKeys, $values); 
     388          } 
     389 
     390          $dumpData[$tableName][$pk] = $values; 
     391        } 
    322392      } 
    323393    } 
     
    382452    return $classes; 
    383453  } 
     454   
     455  protected function fixOrderingOfForeignKeyDataInSameTable($resultsSets, $tableName, $column, $in = null) 
     456  { 
     457    $rs = $this->con->executeQuery(sprintf('SELECT * FROM %s WHERE %s %s', 
     458      constant($tableName.'Peer::TABLE_NAME'), 
     459      strtolower($column->getColumnName()), 
     460      is_null($in) ? 'IS NULL' : 'IN ('.$in.')' 
     461    )); 
     462    $in = array(); 
     463    while ($rs->next()) 
     464    { 
     465      $in[] = "'".$rs->get(strtolower($column->getRelatedColumnName()))."'"; 
     466    } 
     467 
     468    if ($in = implode(', ', $in)) 
     469    { 
     470      $rs->seek(0); 
     471      $resultsSets[] = $rs; 
     472      $resultsSets = $this->fixOrderingOfForeignKeyDataInSameTable($resultsSets, $tableName, $column, $in); 
     473    } 
     474 
     475    return $resultsSets; 
     476  } 
    384477} 

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.