// adjust state, remove from identity map and inform postDelete listeners
foreach ($deletedRecords as $record) {
- // currently just for bc!
- $this->_deleteCTIParents($table, $record);
- //--
$record->state(IPF_ORM_Record::STATE_TCLEAN);
$record->getTable()->removeRecord($record);
$this->_postDelete($record);
if ( ! $event->skipOperation) {
$identifier = $record->identifier();
- if ($table->getOption('joinedParents')) {
- // currrently just for bc!
- $this->_updateCTIRecord($table, $record);
- //--
- } else {
- $array = $record->getPrepared();
- $this->conn->update($table, $array, $identifier);
- }
+ $array = $record->getPrepared();
+ $this->conn->update($table, $array, $identifier);
$record->assignIdentifier(true);
}
$table->notifyRecordListeners('preInsert', $event);
if ( ! $event->skipOperation) {
- if ($table->getOption('joinedParents')) {
- // just for bc!
- $this->_insertCTIRecord($table, $record);
- //--
- } else {
- $this->processSingleInsert($record);
- }
+ $this->processSingleInsert($record);
}
$table->addRecord($record);
return array_values($flushList);
}
- private function _deleteCTIParents(IPF_ORM_Table $table, $record)
- {
- if ($table->getOption('joinedParents')) {
- foreach (array_reverse($table->getOption('joinedParents')) as $parent) {
- $parentTable = $table->getConnection()->getTable($parent);
- $this->conn->delete($parentTable, $record->identifier());
- }
- }
- }
-
- private function _insertCTIRecord(IPF_ORM_Table $table, IPF_ORM_Record $record)
- {
- $dataSet = $this->_formatDataSet($record);
- $component = $table->getComponentName();
-
- $classes = $table->getOption('joinedParents');
- $classes[] = $component;
-
- foreach ($classes as $k => $parent) {
- if ($k === 0) {
- $rootRecord = new $parent();
- $rootRecord->merge($dataSet[$parent]);
- $this->processSingleInsert($rootRecord);
- $record->assignIdentifier($rootRecord->identifier());
- } else {
- foreach ((array) $rootRecord->identifier() as $id => $value) {
- $dataSet[$parent][$id] = $value;
- }
-
- $this->conn->insert($this->conn->getTable($parent), $dataSet[$parent]);
- }
- }
- }
-
- private function _updateCTIRecord(IPF_ORM_Table $table, IPF_ORM_Record $record)
- {
- $identifier = $record->identifier();
- $dataSet = $this->_formatDataSet($record);
-
- $component = $table->getComponentName();
-
- $classes = $table->getOption('joinedParents');
- $classes[] = $component;
-
- foreach ($record as $field => $value) {
- if ($value instanceof IPF_ORM_Record) {
- if ( ! $value->exists()) {
- $value->save();
- }
- $record->set($field, $value->getIncremented());
- }
- }
-
- foreach ($classes as $class) {
- $parentTable = $this->conn->getTable($class);
-
- if ( ! array_key_exists($class, $dataSet)) {
- continue;
- }
-
- $this->conn->update($this->conn->getTable($class), $dataSet[$class], $identifier);
- }
- }
-
private function _formatDataSet(IPF_ORM_Record $record)
{
$table = $record->getTable();
return $dataSet;
}
}
+
$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']);
$sql = array();
foreach ($fields as $fieldName) {
$columnName = $table->getColumnName($fieldName);
- if (($owner = $table->getColumnOwner($columnName)) !== null &&
- $owner !== $table->getComponentName()) {
-
- $parent = $this->_conn->getTable($owner);
- $columnName = $parent->getColumnName($fieldName);
- $parentAlias = $this->getTableAlias($componentAlias . '.' . $parent->getComponentName());
- $sql[] = $this->_conn->quoteIdentifier($parentAlias . '.' . $columnName)
- . ' AS '
- . $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
- } else {
- $columnName = $table->getColumnName($fieldName);
- $sql[] = $this->_conn->quoteIdentifier($tableAlias . '.' . $columnName)
- . ' AS '
- . $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
- }
+ $sql[] = $this->_conn->quoteIdentifier($tableAlias . '.' . $columnName)
+ . ' AS '
+ . $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
}
$this->_neededTables[] = $tableAlias;
// get the short alias for this table
$tableAlias = $this->getTableAlias($componentAlias, $tableName);
- $queryPart = '';
-
- foreach ($table->getOption('joinedParents') as $parent) {
- $parentTable = $this->_conn->getTable($parent);
-
- $parentAlias = $componentAlias . '.' . $parent;
-
- // get the short alias for the parent table
- $parentTableAlias = $this->getTableAlias($parentAlias, $parentTable->getTableName());
-
- $queryPart .= ' LEFT JOIN ' . $this->_conn->quoteIdentifier($parentTable->getTableName())
- . ' ' . $this->_conn->quoteIdentifier($parentTableAlias) . ' ON ';
-
- //IPF_ORM::dump($table->getIdentifier());
- foreach ((array) $table->getIdentifier() as $identifier) {
- $column = $table->getColumnName($identifier);
-
- $queryPart .= $this->_conn->quoteIdentifier($tableAlias)
- . '.' . $this->_conn->quoteIdentifier($column)
- . ' = ' . $this->_conn->quoteIdentifier($parentTableAlias)
- . '.' . $this->_conn->quoteIdentifier($column);
- }
- }
-
- return $queryPart;
+ return '';
}
public function getCountQuery()
'collation' => null,
'indexes' => array(),
'parents' => array(),
- 'joinedParents' => array(),
'queryParts' => array(),
'versioning' => null,
'subclasses' => array(),
$class = new ReflectionClass($class);
}
- $this->_options['joinedParents'] = array();
-
- foreach (array_reverse($this->_options['parents']) as $parent) {
-
- if ($parent === $class->getName()) {
- continue;
- }
- $ref = new ReflectionClass($parent);
-
- if ($ref->isAbstract()) {
- continue;
- }
- $parentTable = $this->_conn->getTable($parent);
-
- $found = false;
- $parentColumns = $parentTable->getColumns();
-
- foreach ($parentColumns as $columnName => $definition) {
- if ( ! isset($definition['primary']) || $definition['primary'] === false) {
- if (isset($this->_columns[$columnName])) {
- $found = true;
- break;
- } else {
- if ( ! isset($parentColumns[$columnName]['owner'])) {
- $parentColumns[$columnName]['owner'] = $parentTable->getComponentName();
- }
-
- $this->_options['joinedParents'][] = $parentColumns[$columnName]['owner'];
- }
- } else {
- unset($parentColumns[$columnName]);
- }
- }
-
- if ($found) {
- continue;
- }
-
- foreach ($parentColumns as $columnName => $definition) {
- $fullName = $columnName . ' as ' . $parentTable->getFieldName($columnName);
- $this->setColumn($fullName, $definition['type'], $definition['length'], $definition, true);
- }
-
- break;
- }
-
- $this->_options['joinedParents'] = array_values(array_unique($this->_options['joinedParents']));
-
$this->_options['declaringClass'] = $class;
$this->columnCount = count($this->_columns);
{
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() !== IPF_ORM::IDENTIFIER_AUTOINC)
- ? $table->getIdentifierType() : IPF_ORM::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
- $fullName = $id . ' as ' . $table->getFieldName($id);
- $this->setColumn($fullName, $definition['type'], $definition['length'],
- $definition, true);
- }
- } else {
- $definition = array('type' => 'integer',
- 'length' => 20,
- 'autoincrement' => true,
- 'primary' => true);
- $this->setColumn('id', $definition['type'], $definition['length'], $definition, true);
- $this->_identifier = 'id';
- $this->_identifierType = IPF_ORM::IDENTIFIER_AUTOINC;
- }
+ $definition = array('type' => 'integer',
+ 'length' => 20,
+ 'autoincrement' => true,
+ 'primary' => true);
+ $this->setColumn('id', $definition['type'], $definition['length'], $definition, true);
+ $this->_identifier = 'id';
+ $this->_identifierType = IPF_ORM::IDENTIFIER_AUTOINC;
$this->columnCount++;
break;
case 1:
}
}
- public function getColumnOwner($columnName)
- {
- if (isset($this->_columns[$columnName]['owner'])) {
- return $this->_columns[$columnName]['owner'];
- } else {
- return $this->getComponentName();
- }
- }
-
- public function getRecordInstance()
- {
- if ( ! $this->record) {
- $this->record = new $this->_options['name'];
- }
- return $this->record;
- }
-
- public function isInheritedColumn($columnName)
- {
- return (isset($this->_columns[$columnName]['owner']));
- }
-
public function isIdentifier($fieldName)
{
return ($fieldName === $this->getIdentifier() ||