1
0
mirror of synced 2025-03-04 20:03:21 +03:00

Rewrote data loading to support I18n data, nested set data, and better relationship defining for fixtures. Also fixes ticket:528

This commit is contained in:
Jonathan.Wage 2007-11-08 18:59:48 +00:00
parent 37898ac06a
commit 0038138095

View File

@ -32,6 +32,8 @@
*/ */
class Doctrine_Data_Import extends Doctrine_Data class Doctrine_Data_Import extends Doctrine_Data
{ {
private $_importedObjects = array();
/** /**
* constructor * constructor
* *
@ -81,6 +83,27 @@ class Doctrine_Data_Import extends Doctrine_Data
$this->loadData($array); $this->loadData($array);
} }
protected function buildRows($className, $data)
{
$rows = array();
foreach ($data as $rowKey => $row) {
// do the same for the row information
$rows[$className][$rowKey] = $row;
foreach ($row as $key => $value) {
if (Doctrine::getTable($className)->hasRelation($key) && is_array($value)) {
$keys = array_keys($value);
// Skip associative arrays defining keys to relationships
if (!isset($keys[0])) {
$rows = array_merge($rows, $this->buildRows(Doctrine::getTable($className)->getRelation($key)->getTable()->getOption('name'), $value));
}
}
}
}
return $rows;
}
/** /**
* loadData * loadData
* *
@ -90,10 +113,7 @@ class Doctrine_Data_Import extends Doctrine_Data
protected function loadData(array $array) protected function loadData(array $array)
{ {
$specifiedModels = $this->getModels(); $specifiedModels = $this->getModels();
$rows = array();
$pendingRelations = array();
$primaryKeys = array();
foreach ($array as $className => $data) { foreach ($array as $className => $data) {
@ -101,50 +121,96 @@ class Doctrine_Data_Import extends Doctrine_Data
continue; continue;
} }
foreach ($data as $rowKey => $row) { // This is simple here to get the templates present for this model
// better way?
$obj = new $className(); $obj = new $className();
$templates = array_keys($obj->getTable()->getTemplates());
if (in_array('Doctrine_Template_NestedSet', $templates)) {
$this->loadNestedSetData($className, $data);
} else {
$rows = array_merge($rows, $this->buildRows($className, $data));
}
}
$buildRows = array();
foreach ($rows as $className => $classRows) {
foreach ($classRows as $rowKey => $row) {
$buildRows[$rowKey] = $row;
$this->_importedObjects[$rowKey] = new $className();
}
}
foreach($buildRows as $rowKey => $row) {
$obj = $this->_importedObjects[$rowKey];
foreach ($row as $key => $value) { foreach ($row as $key => $value) {
// If row key is a relation store it for later fixing once we have all primary keys if ($obj->getTable()->hasColumn($key)) {
if ($obj->getTable()->hasRelation($key)) { $obj->set($key, $value);
$relation = $obj->getTable()->getRelation($key); } else if ($obj->getTable()->hasRelation($key)) {
if (is_array($value)) {
if (isset($value[0])) {
foreach ($value as $link) {
$pendingRelations[] = array('key' => $value, 'obj' => $obj, 'local' => $relation['local'], 'foreign' => $relation['foreign']); if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::ONE) {
// If we have a normal column $obj->set($key, $this->_importedObjects[$link]);
} else if ($obj->getTable()->hasColumn($key)) { } else if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) {
$obj->$key = $value; $relation = $obj->$key;
// Otherwise lets move on
$relation[] = $this->_importedObjects[$link];
}
}
} else { } else {
continue; $obj->$key->fromArray($value);
}
} else if (isset($this->_importedObjects[$value])) {
$obj->set($key, $this->_importedObjects[$value]);
}
}
} }
} }
$identifier = is_array($obj->getTable()->getIdentifier()) ? $obj->getTable()->getIdentifier():array($obj->getTable()->getIdentifier()); $manager = Doctrine_Manager::getInstance();
foreach ($manager as $connection) {
// We only want to save the record if it is a single primary key. $connection->flush();
// We can't save the composite primary key because neither of the foreign keys have been created yet
// We will satisfy these relationships later and save the record.
if (count($identifier) === 1) {
$obj->save();
}
$primaryKeys[$rowKey] = $obj->identifier();
} }
} }
// Satisfy all relationships protected function loadNestedSetData($model, $nestedSetData, $parent = null)
foreach ($pendingRelations as $rowKey => $pending) { {
$obj = $pending['obj']; $manager = Doctrine_Manager::getInstance();
$key = $pending['key'];
$local = $pending['local']; foreach($nestedSetData AS $rowKey => $nestedSet)
$pks = $primaryKeys[$key]; {
$obj->$local = $pks['id']; $children = array();
$data = array();
if( array_key_exists('children', $nestedSet) )
{
$children = $nestedSet['children'];
unset($nestedSet['children']);
} }
// Loop over all again to save them since we satisfied all pending relationships above $record = new $model();
foreach ($pendingRelations as $rowKey => $pending) {
$obj = $pending['obj']; $this->_importedObjects[$rowKey] = $record;
$obj->save();
if( is_array($nestedSet) AND !empty($nestedSet) )
{
$record->fromArray($nestedSet);
}
if( !$parent )
{
$manager->getTable($model)->getTree()->createRoot($record);
} else {
$parent->getNode()->addChild($record);
}
if( is_array($children) AND !empty($children) )
{
$this->loadNestedSetData($model, $children, $record);
}
} }
} }