Esempio n. 1
0
    /**
     * Deletes all records from this collection
     *
     * @return Doctrine_Collection
     */
    public function delete(Doctrine_Connection $conn = null, $clearColl = true)
    {
        if ($conn == null) {
            $conn = $this->_table->getConnection();
        }

        try {
            $conn->beginInternalTransaction();
            $conn->transaction->addCollection($this);

            foreach ($this as $key => $record) {
                $record->delete($conn);
            }

            $conn->commit();
        } catch (Exception $e) {
            $conn->rollback();
            throw $e;
        }

        if ($clearColl) {
            $this->clear();
        }

        return $this;
    }
Esempio n. 2
0
 public function parseFunction($dql)
 {
     if (($pos = strpos($dql, '(')) !== false) {
         $func = substr($dql, 0, $pos);
         $value = substr($dql, $pos + 1, -1);
         $expr = $this->table->getConnection()->expression;
         if (!method_exists($expr, $func)) {
             throw new Doctrine_Query_Exception('Unknown function ' . $func);
         }
         $func = $expr->{$func}($value);
     }
     return $func;
 }
Esempio n. 3
0
 /**
  * delete
  * single shot delete
  * deletes all records from this collection
  * and uses only one database query to perform this operation
  *
  * @return Doctrine_Collection
  */
 public function delete(Doctrine_Connection $conn = null)
 {
     if ($conn == null) {
         $conn = $this->_table->getConnection();
     }
     $conn->beginTransaction();
     $conn->transaction->addCollection($this);
     foreach ($this as $key => $record) {
         $record->delete($conn);
     }
     $conn->commit();
     $this->data = array();
     return $this;
 }
Esempio n. 4
0
 /**
  * Completes the given definition
  *
  * @param array $def    definition array to be completed
  * @return array        completed definition array
  * @todo Description: What does it mean to complete a definition? What is done (not how)?
  *       Refactor (too long & nesting level)
  */
 public function completeDefinition($def)
 {
     $conn = $this->_table->getConnection();
     $def['table'] = $this->getImpl($def['class']);
     $def['localTable'] = $this->_table;
     $def['class'] = $def['table']->getComponentName();
     $foreignClasses = array_merge($def['table']->getOption('parents'), array($def['class']));
     $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
     $localIdentifierColumnNames = $this->_table->getIdentifierColumnNames();
     $localIdentifierCount = count($localIdentifierColumnNames);
     $localIdColumnName = array_pop($localIdentifierColumnNames);
     $foreignIdentifierColumnNames = $def['table']->getIdentifierColumnNames();
     $foreignIdColumnName = array_pop($foreignIdentifierColumnNames);
     if (isset($def['local'])) {
         $def['local'] = $def['localTable']->getColumnName($def['local']);
         if (!isset($def['foreign'])) {
             // local key is set, but foreign key is not
             // try to guess the foreign key
             if ($def['local'] === $localIdColumnName) {
                 $def['foreign'] = $this->guessColumns($localClasses, $def['table']);
             } else {
                 // the foreign field is likely to be the
                 // identifier of the foreign class
                 $def['foreign'] = $foreignIdColumnName;
                 $def['localKey'] = true;
             }
         } else {
             $def['foreign'] = $def['table']->getColumnName($def['foreign']);
             if ($localIdentifierCount == 1) {
                 if ($def['local'] == $localIdColumnName && isset($def['owningSide']) && $def['owningSide'] === true) {
                     $def['localKey'] = true;
                 } else {
                     if ($def['local'] !== $localIdColumnName && $def['type'] == Doctrine_Relation::ONE) {
                         $def['localKey'] = true;
                     }
                 }
             } else {
                 if ($localIdentifierCount > 1 && !isset($def['localKey'])) {
                     // It's a composite key and since 'foreign' can not point to a composite
                     // key currently, we know that 'local' must be the foreign key.
                     $def['localKey'] = true;
                 }
             }
         }
     } else {
         if (isset($def['foreign'])) {
             $def['foreign'] = $def['table']->getColumnName($def['foreign']);
             // local key not set, but foreign key is set
             // try to guess the local key
             if ($def['foreign'] === $foreignIdColumnName) {
                 $def['localKey'] = true;
                 try {
                     $def['local'] = $this->guessColumns($foreignClasses, $this->_table);
                 } catch (Doctrine_Relation_Exception $e) {
                     $def['local'] = $localIdColumnName;
                 }
             } else {
                 $def['local'] = $localIdColumnName;
             }
         } else {
             // neither local or foreign key is being set
             // try to guess both keys
             $conn = $this->_table->getConnection();
             // the following loops are needed for covering inheritance
             foreach ($localClasses as $class) {
                 $table = $conn->getTable($class);
                 $identifierColumnNames = $table->getIdentifierColumnNames();
                 $idColumnName = array_pop($identifierColumnNames);
                 $column = strtolower($table->getComponentName()) . '_' . $idColumnName;
                 foreach ($foreignClasses as $class2) {
                     $table2 = $conn->getTable($class2);
                     if ($table2->hasColumn($column)) {
                         $def['foreign'] = $column;
                         $def['local'] = $idColumnName;
                         return $def;
                     }
                 }
             }
             foreach ($foreignClasses as $class) {
                 $table = $conn->getTable($class);
                 $identifierColumnNames = $table->getIdentifierColumnNames();
                 $idColumnName = array_pop($identifierColumnNames);
                 $column = strtolower($table->getComponentName()) . '_' . $idColumnName;
                 foreach ($localClasses as $class2) {
                     $table2 = $conn->getTable($class2);
                     if ($table2->hasColumn($column)) {
                         $def['foreign'] = $idColumnName;
                         $def['local'] = $column;
                         $def['localKey'] = true;
                         return $def;
                     }
                 }
             }
             // auto-add columns and auto-build relation
             $columns = array();
             foreach ((array) $this->_table->getIdentifierColumnNames() as $id) {
                 // ?? should this not be $this->_table->getComponentName() ??
                 $column = strtolower($table->getComponentName()) . '_' . $id;
                 $col = $this->_table->getColumnDefinition($id);
                 $type = $col['type'];
                 $length = $col['length'];
                 unset($col['type']);
                 unset($col['length']);
                 unset($col['autoincrement']);
                 unset($col['sequence']);
                 unset($col['primary']);
                 $def['table']->setColumn($column, $type, $length, $col);
                 $columns[] = $column;
             }
             if (count($columns) > 1) {
                 $def['foreign'] = $columns;
             } else {
                 $def['foreign'] = $columns[0];
             }
             $def['local'] = $localIdColumnName;
         }
     }
     return $def;
 }
Esempio n. 5
0
 /**
  * Class Table Inheritance code.
  * Support dropped for 0.10/1.0.
  *
  * Note: This is flawed. We also need to delete from subclass tables.
  */
 private function _deleteCTIParents(Doctrine_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());
         }
     }
 }
Esempio n. 6
0
 /**
  * Completes the given definition
  *
  * @param array $def    definition array to be completed
  * @return array        completed definition array
  */
 public function completeDefinition($def)
 {
     $conn = $this->_table->getConnection();
     $def['table'] = $conn->getTable($def['class']);
     $foreignClasses = array_merge($def['table']->getOption('parents'), array($def['class']));
     $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
     if (isset($def['local'])) {
         if (!isset($def['foreign'])) {
             // local key is set, but foreign key is not
             // try to guess the foreign key
             if ($def['local'] === $this->_table->getIdentifier()) {
                 $def['foreign'] = $this->guessColumns($localClasses, $def['table']);
             } else {
                 // the foreign field is likely to be the
                 // identifier of the foreign class
                 $def['foreign'] = $def['table']->getIdentifier();
                 $def['localKey'] = true;
             }
         } else {
             if ($def['local'] !== $this->_table->getIdentifier()) {
                 $def['localKey'] = true;
             }
         }
     } else {
         if (isset($def['foreign'])) {
             // local key not set, but foreign key is set
             // try to guess the local key
             if ($def['foreign'] === $def['table']->getIdentifier()) {
                 $def['localKey'] = true;
                 try {
                     $def['local'] = $this->guessColumns($foreignClasses, $this->_table);
                 } catch (Doctrine_Relation_Exception $e) {
                     $def['local'] = $this->_table->getIdentifier();
                 }
             } else {
                 $def['local'] = $this->_table->getIdentifier();
             }
         } else {
             // neither local or foreign key is being set
             // try to guess both keys
             $conn = $this->_table->getConnection();
             // the following loops are needed for covering inheritance
             foreach ($localClasses as $class) {
                 $table = $conn->getTable($class);
                 $column = strtolower($table->getComponentName()) . '_' . $table->getIdentifier();
                 foreach ($foreignClasses as $class2) {
                     $table2 = $conn->getTable($class2);
                     if ($table2->hasColumn($column)) {
                         $def['foreign'] = $column;
                         $def['local'] = $table->getIdentifier();
                         return $def;
                     }
                 }
             }
             foreach ($foreignClasses as $class) {
                 $table = $conn->getTable($class);
                 $column = strtolower($table->getComponentName()) . '_' . $table->getIdentifier();
                 foreach ($localClasses as $class2) {
                     $table2 = $conn->getTable($class2);
                     if ($table2->hasColumn($column)) {
                         $def['foreign'] = $table->getIdentifier();
                         $def['local'] = $column;
                         $def['localKey'] = true;
                         return $def;
                     }
                 }
             }
             // auto-add columns and auto-build relation
             $columns = array();
             foreach ((array) $this->_table->getIdentifier() as $id) {
                 $column = strtolower($table->getComponentName()) . '_' . $id;
                 $col = $this->_table->getDefinitionOf($id);
                 $type = $col['type'];
                 $length = $col['length'];
                 unset($col['type']);
                 unset($col['length']);
                 unset($col['autoincrement']);
                 unset($col['sequence']);
                 unset($col['primary']);
                 $def['table']->setColumn($column, $type, $length, $col);
                 $columns[] = $column;
             }
             if (count($columns) > 1) {
                 $def['foreign'] = $columns;
             } else {
                 $def['foreign'] = $columns[0];
             }
             $def['local'] = $this->_table->getIdentifier();
         }
     }
     return $def;
 }