Merged r3132:3133 fixed #591
This commit is contained in:
parent
46f2277094
commit
e53a659c6e
@ -32,7 +32,8 @@
|
||||
*/
|
||||
class Doctrine_Data_Import extends Doctrine_Data
|
||||
{
|
||||
private $_importedObjects = array();
|
||||
protected $_importedObjects = array();
|
||||
protected $_rows = array();
|
||||
|
||||
/**
|
||||
* constructor
|
||||
@ -85,10 +86,9 @@ class Doctrine_Data_Import extends Doctrine_Data
|
||||
|
||||
protected function _buildRows($className, $data)
|
||||
{
|
||||
$rows = array();
|
||||
foreach ($data as $rowKey => $row) {
|
||||
// do the same for the row information
|
||||
$rows[$className][$rowKey] = $row;
|
||||
$this->_rows[$className][$rowKey] = $row;
|
||||
|
||||
foreach ($row as $key => $value) {
|
||||
if (Doctrine::getTable($className)->hasRelation($key) && is_array($value)) {
|
||||
@ -96,52 +96,26 @@ class Doctrine_Data_Import extends Doctrine_Data
|
||||
|
||||
// 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));
|
||||
$this->_buildRows(Doctrine::getTable($className)->getRelation($key)->getTable()->getOption('name'), $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
/**
|
||||
* loadData
|
||||
*
|
||||
* @param string $array
|
||||
* @return void
|
||||
*/
|
||||
protected function _loadData(array $array)
|
||||
protected function _buildNestedSetRows($className, $data)
|
||||
{
|
||||
$specifiedModels = $this->getModels();
|
||||
$rows = array();
|
||||
foreach ($data as $rowKey => $row) {
|
||||
$children = isset($row['children']) ? $row['children']:array();
|
||||
unset($row['children']);
|
||||
$this->_rows[$className][$rowKey] = $row;
|
||||
|
||||
foreach ($array as $className => $data) {
|
||||
|
||||
if ( ! empty($specifiedModels) && !in_array($className, $specifiedModels)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// This is simple here to get the templates present for this model
|
||||
// better way?
|
||||
$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));
|
||||
$this->_buildNestedSetRows($className, $children);
|
||||
}
|
||||
}
|
||||
|
||||
$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) {
|
||||
protected function _processRow($rowKey, $row)
|
||||
{
|
||||
$obj = $this->_importedObjects[$rowKey];
|
||||
|
||||
foreach ($row as $key => $value) {
|
||||
@ -170,6 +144,50 @@ class Doctrine_Data_Import extends Doctrine_Data
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* loadData
|
||||
*
|
||||
* @param string $array
|
||||
* @return void
|
||||
*/
|
||||
protected function _loadData(array $array)
|
||||
{
|
||||
$nestedSets = array();
|
||||
|
||||
$specifiedModels = $this->getModels();
|
||||
$rows = array();
|
||||
|
||||
foreach ($array as $className => $data) {
|
||||
|
||||
if ( ! empty($specifiedModels) && !in_array($className, $specifiedModels)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// This is simple here to get the templates present for this model
|
||||
// better way?
|
||||
$obj = new $className(null, true);
|
||||
$templates = array_keys($obj->getTable()->getTemplates());
|
||||
|
||||
if (in_array('Doctrine_Template_NestedSet', $templates)) {
|
||||
$nestedSets[$className][] = $data;
|
||||
$this->_buildNestedSetRows($className, $data);
|
||||
} else {
|
||||
$this->_buildRows($className, $data);
|
||||
}
|
||||
}
|
||||
|
||||
$buildRows = array();
|
||||
foreach ($this->_rows as $className => $classRows) {
|
||||
foreach ($classRows as $rowKey => $row) {
|
||||
$buildRows[$rowKey] = $row;
|
||||
$this->_importedObjects[$rowKey] = new $className();
|
||||
}
|
||||
}
|
||||
|
||||
foreach($buildRows as $rowKey => $row) {
|
||||
$this->_processRow($rowKey, $row);
|
||||
}
|
||||
|
||||
$manager = Doctrine_Manager::getInstance();
|
||||
foreach ($manager as $connection) {
|
||||
$objects = array();
|
||||
@ -181,12 +199,20 @@ class Doctrine_Data_Import extends Doctrine_Data
|
||||
|
||||
foreach ($tree as $model) {
|
||||
foreach ($this->_importedObjects as $obj) {
|
||||
if ($obj instanceof $model) {
|
||||
$templates = array_keys($obj->getTable()->getTemplates());
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function _loadNestedSetData($model, $nestedSetData, $parent = null)
|
||||
@ -204,13 +230,11 @@ class Doctrine_Data_Import extends Doctrine_Data
|
||||
unset($nestedSet['children']);
|
||||
}
|
||||
|
||||
$record = new $model();
|
||||
|
||||
$this->_importedObjects[$rowKey] = $record;
|
||||
$record = $this->_importedObjects[$rowKey];
|
||||
|
||||
if( is_array($nestedSet) AND !empty($nestedSet) )
|
||||
{
|
||||
$record->fromArray($nestedSet);
|
||||
$this->_processRow($rowKey, $nestedSet);
|
||||
}
|
||||
|
||||
if( !$parent )
|
||||
|
Loading…
Reference in New Issue
Block a user