1
0
mirror of synced 2025-01-06 00:57:10 +03:00

Merged r3132:3133 fixed #591

This commit is contained in:
Jonathan.Wage 2007-11-10 21:39:36 +00:00
parent 46f2277094
commit e53a659c6e

View File

@ -32,8 +32,9 @@
*/ */
class Doctrine_Data_Import extends Doctrine_Data class Doctrine_Data_Import extends Doctrine_Data
{ {
private $_importedObjects = array(); protected $_importedObjects = array();
protected $_rows = array();
/** /**
* constructor * constructor
* *
@ -85,10 +86,9 @@ class Doctrine_Data_Import extends Doctrine_Data
protected function _buildRows($className, $data) protected function _buildRows($className, $data)
{ {
$rows = array();
foreach ($data as $rowKey => $row) { foreach ($data as $rowKey => $row) {
// do the same for the row information // do the same for the row information
$rows[$className][$rowKey] = $row; $this->_rows[$className][$rowKey] = $row;
foreach ($row as $key => $value) { foreach ($row as $key => $value) {
if (Doctrine::getTable($className)->hasRelation($key) && is_array($value)) { if (Doctrine::getTable($className)->hasRelation($key) && is_array($value)) {
@ -96,14 +96,54 @@ class Doctrine_Data_Import extends Doctrine_Data
// Skip associative arrays defining keys to relationships // Skip associative arrays defining keys to relationships
if (!isset($keys[0])) { if (!isset($keys[0])) {
$rows = array_merge($rows, $this->_buildRows(Doctrine::getTable($className)->getRelation($key)->getTable()->getOption('name'), $value)); $this->_buildRows(Doctrine::getTable($className)->getRelation($key)->getTable()->getOption('name'), $value);
} }
} }
} }
} }
return $rows;
} }
protected function _buildNestedSetRows($className, $data)
{
foreach ($data as $rowKey => $row) {
$children = isset($row['children']) ? $row['children']:array();
unset($row['children']);
$this->_rows[$className][$rowKey] = $row;
$this->_buildNestedSetRows($className, $children);
}
}
protected function _processRow($rowKey, $row)
{
$obj = $this->_importedObjects[$rowKey];
foreach ($row as $key => $value) {
if ($obj->getTable()->hasColumn($key)) {
$obj->set($key, $value);
} else if ($obj->getTable()->hasRelation($key)) {
if (is_array($value)) {
if (isset($value[0])) {
foreach ($value as $link) {
if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::ONE) {
$obj->set($key, $this->_importedObjects[$link]);
} else if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) {
$relation = $obj->$key;
$relation[] = $this->_importedObjects[$link];
}
}
} else {
$obj->$key->fromArray($value);
}
} else if (isset($this->_importedObjects[$value])) {
$obj->set($key, $this->_importedObjects[$value]);
}
}
}
}
/** /**
* loadData * loadData
* *
@ -112,6 +152,8 @@ class Doctrine_Data_Import extends Doctrine_Data
*/ */
protected function _loadData(array $array) protected function _loadData(array $array)
{ {
$nestedSets = array();
$specifiedModels = $this->getModels(); $specifiedModels = $this->getModels();
$rows = array(); $rows = array();
@ -123,51 +165,27 @@ class Doctrine_Data_Import extends Doctrine_Data
// This is simple here to get the templates present for this model // This is simple here to get the templates present for this model
// better way? // better way?
$obj = new $className(); $obj = new $className(null, true);
$templates = array_keys($obj->getTable()->getTemplates()); $templates = array_keys($obj->getTable()->getTemplates());
if (in_array('Doctrine_Template_NestedSet', $templates)) { if (in_array('Doctrine_Template_NestedSet', $templates)) {
$this->_loadNestedSetData($className, $data); $nestedSets[$className][] = $data;
$this->_buildNestedSetRows($className, $data);
} else { } else {
$rows = array_merge($rows, $this->_buildRows($className, $data)); $this->_buildRows($className, $data);
} }
} }
$buildRows = array(); $buildRows = array();
foreach ($rows as $className => $classRows) { foreach ($this->_rows as $className => $classRows) {
foreach ($classRows as $rowKey => $row) { foreach ($classRows as $rowKey => $row) {
$buildRows[$rowKey] = $row; $buildRows[$rowKey] = $row;
$this->_importedObjects[$rowKey] = new $className(); $this->_importedObjects[$rowKey] = new $className();
} }
} }
foreach($buildRows as $rowKey => $row) { foreach($buildRows as $rowKey => $row) {
$obj = $this->_importedObjects[$rowKey]; $this->_processRow($rowKey, $row);
foreach ($row as $key => $value) {
if ($obj->getTable()->hasColumn($key)) {
$obj->set($key, $value);
} else if ($obj->getTable()->hasRelation($key)) {
if (is_array($value)) {
if (isset($value[0])) {
foreach ($value as $link) {
if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::ONE) {
$obj->set($key, $this->_importedObjects[$link]);
} else if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) {
$relation = $obj->$key;
$relation[] = $this->_importedObjects[$link];
}
}
} else {
$obj->$key->fromArray($value);
}
} else if (isset($this->_importedObjects[$value])) {
$obj->set($key, $this->_importedObjects[$value]);
}
}
}
} }
$manager = Doctrine_Manager::getInstance(); $manager = Doctrine_Manager::getInstance();
@ -180,11 +198,19 @@ class Doctrine_Data_Import extends Doctrine_Data
$tree = $connection->unitOfWork->buildFlushTree($objects); $tree = $connection->unitOfWork->buildFlushTree($objects);
foreach ($tree as $model) { foreach ($tree as $model) {
foreach ($this->_importedObjects as $obj) { foreach ($this->_importedObjects as $obj) {
if ($obj instanceof $model) { $templates = array_keys($obj->getTable()->getTemplates());
$obj->save();
if ($obj instanceof $model && !in_array('Doctrine_Template_NestedSet', $templates)) {
$obj->save();
}
} }
} }
}
foreach ($nestedSets as $className => $sets) {
foreach ($sets as $data) {
$this->_loadNestedSetData($className, $data);
} }
} }
} }
@ -204,13 +230,11 @@ class Doctrine_Data_Import extends Doctrine_Data
unset($nestedSet['children']); unset($nestedSet['children']);
} }
$record = new $model(); $record = $this->_importedObjects[$rowKey];
$this->_importedObjects[$rowKey] = $record;
if( is_array($nestedSet) AND !empty($nestedSet) ) if( is_array($nestedSet) AND !empty($nestedSet) )
{ {
$record->fromArray($nestedSet); $this->_processRow($rowKey, $nestedSet);
} }
if( !$parent ) if( !$parent )