Commit 5bb44938 authored by zYne's avatar zYne

Drafting the initial CTI support, only works for simple inserts now

parent d1c23818
......@@ -586,9 +586,63 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
$record->preInsert($event);
$record->getTable()->getRecordListener()->preInsert($event);
$table = $record->getTable();
$table->getRecordListener()->preInsert($event);
if ( ! $event->skipOperation) {
if (count($table->getOption('joinedParents')) > 0) {
$dataSet = array();
$component = $table->getComponentName();
$array = $record->getPrepared();
foreach ($table->getColumns() as $column => $definition) {
if (isset($definition['primary']) && $definition['primary']) {
continue;
}
if (isset($definition['owner'])) {
$dataSet[$definition['owner']][$column] = $array[$column];
} else {
$dataSet[$component][$column] = $array[$column];
}
}
$classes = $table->getOption('joinedParents');
$classes[] = $component;
foreach ($classes as $k => $parent) {
if ($k === 0) {
$rootRecord = new $parent();
$rootRecord->merge($dataSet[$parent]);
$this->processSingleInsert($rootRecord);
} else {
foreach ((array) $rootRecord->identifier() as $id => $value) {
$dataSet[$parent][$id] = $value;
}
$this->conn->insert($this->conn->getTable($parent)->getTableName(), $dataSet[$parent]);
}
}
} else {
$this->processSingleInsert($record);
}
}
$table->addRecord($record);
$table->getRecordListener()->postInsert($event);
$record->postInsert($event);
return true;
}
public function processSingleInsert(Doctrine_Record $record)
{
$array = $record->getPrepared();
if (empty($array)) {
......@@ -627,12 +681,4 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
$record->assignIdentifier(true);
}
}
$record->getTable()->addRecord($record);
$record->getTable()->getRecordListener()->postInsert($event);
$record->postInsert($event);
return true;
}
}
......@@ -1132,6 +1132,16 @@ class Doctrine_Export extends Doctrine_Connection_Module
$record = new $name();
$table = $record->getTable();
$parents = $table->getOption('joinedParents');
foreach ($parents as $parent) {
$data = $table->getConnection()->getTable($parent)->getExportableFormat();
$query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
$sql = array_merge($sql, (array) $query);
}
$data = $table->getExportableFormat();
$query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
......
......@@ -35,7 +35,7 @@
class Doctrine_Table extends Doctrine_Configurable implements Countable
{
/**
* @var array $data temporary data which is then loaded into Doctrine_Record::$data
* @var array $data temporary data which is then loaded into Doctrine_Record::$_data
*/
protected $_data = array();
......@@ -257,6 +257,48 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
} else {
$class = new ReflectionClass($class);
}
$this->_options['joinedParents'] = array();
foreach (array_reverse($this->_options['parents']) as $parent) {
if ($parent === $class->getName()) {
continue;
}
$table = $this->_conn->getTable($parent);
$found = false;
$columns = $table->getColumns();
foreach ($columns as $column => $definition) {
if ( ! isset($definition['primary'])) {
if (isset($this->_columns[$column])) {
$found = true;
break;
} else {
if ( ! isset($columns[$column]['owner'])) {
$columns[$column]['owner'] = $table->getComponentName();
}
$this->_options['joinedParents'][] = $columns[$column]['owner'];
}
} else {
unset($columns[$column]);
}
}
if ($found) {
continue;
}
$this->_columns = array_merge($columns, $this->_columns);
break;
}
$this->_options['joinedParents'] = array_values(array_unique($this->_options['joinedParents']));
$this->_options['declaringClass'] = $class;
// set the table definition for the given tree implementation
......@@ -276,6 +318,30 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
{
switch (count($this->_identifier)) {
case 0:
if ( ! empty($this->_options['joinedParents'])) {
$root = current($this->_options['joinedParents']);
$table = $this->_conn->getTable($root);
$this->_identifier = $table->getIdentifier();
$this->_identifierType = ($table->getIdentifierType() !== Doctrine::IDENTIFIER_AUTOINC)
? $table->getIdentifierType() : Doctrine::IDENTIFIER_NATURAL;
// add all inherited primary keys
foreach ((array) $this->_identifier as $id) {
$definition = $table->getDefinitionOf($id);
// inherited primary keys shouldn't contain autoinc
// and sequence definitions
unset($definition['autoincrement']);
unset($definition['sequence']);
// add the inherited primary key column
$this->_columns = array_merge(array($id => $definition), $this->_columns);
}
} else {
$this->_columns = array_merge(array('id' =>
array('type' => 'integer',
'length' => 20,
......@@ -283,6 +349,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
'primary' => true)), $this->_columns);
$this->_identifier = 'id';
$this->_identifierType = Doctrine::IDENTIFIER_AUTOINC;
}
$this->columnCount++;
break;
case 1:
......@@ -390,6 +457,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
foreach ($this->getColumns() as $name => $column) {
$definition = $column;
if (isset($column['owner'])) {
continue;
}
switch ($definition['type']) {
case 'enum':
if (isset($definition['default'])) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment