/**
  * Checks whether the given managed entity exists in the database.
  *
  * @param object $entity
  * @return boolean TRUE if the entity exists in the database, FALSE otherwise.
  */
 public function exists($entity, array $extraConditions = array())
 {
     $criteria = $this->_class->getIdentifierValues($entity);
     if ($extraConditions) {
         $criteria = array_merge($criteria, $extraConditions);
     }
     $sql = 'SELECT 1 FROM ' . $this->_class->getQuotedTableName($this->_platform) . ' ' . $this->_getSQLTableAlias($this->_class->name) . ' WHERE ' . $this->_getSelectConditionSQL($criteria);
     return (bool) $this->_conn->fetchColumn($sql, array_values($criteria));
 }
 /**
  * Generates the INSERT SQL used by the persister to persist entities.
  * 
  * @return string
  * @internal Result is cached by getInsertSQL() per request.
  */
 protected function _generateInsertSQL()
 {
     $insertSql = '';
     $columns = $this->_getInsertColumnList();
     if (empty($columns)) {
         $insertSql = $this->_platform->getEmptyIdentityInsertSQL($this->_class->getQuotedTableName($this->_platform), $this->_class->getQuotedColumnName($this->_class->identifier[0], $this->_platform));
     } else {
         $columns = array_unique($columns);
         $values = array_fill(0, count($columns), '?');
         $insertSql = 'INSERT INTO ' . $this->_class->getQuotedTableName($this->_platform) . ' (' . implode(', ', $columns) . ') ' . 'VALUES (' . implode(', ', $values) . ')';
     }
     return $insertSql;
 }
 /**
  * Lock all rows of this entity matching the given criteria with the specified pessimistic lock mode
  *
  * @param array $criteria
  * @param int $lockMode
  * @return void
  */
 public function lock(array $criteria, $lockMode)
 {
     $conditionSql = $this->_getSelectConditionSQL($criteria);
     if ($lockMode == \Doctrine\DBAL\LockMode::PESSIMISTIC_READ) {
         $lockSql = $this->_platform->getReadLockSql();
     } else {
         if ($lockMode == \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE) {
             $lockSql = $this->_platform->getWriteLockSql();
         }
     }
     $sql = 'SELECT 1 FROM ' . $this->_class->getQuotedTableName($this->_platform) . ' ' . $this->_getSQLTableAlias($this->_class->name) . ($conditionSql ? ' WHERE ' . $conditionSql : '') . ' ' . $lockSql;
     $params = array_values($criteria);
     $this->_conn->executeQuery($sql, $params);
 }
 /**
  * Get the FROM and optionally JOIN conditions to lock the entity managed by this persister.
  *
  * @return string
  */
 protected function getLockTablesSql()
 {
     return 'FROM ' . $this->_class->getQuotedTableName($this->_platform) . ' ' . $this->_getSQLTableAlias($this->_class->name);
 }
Exemple #5
0
 /**
  * Gather columns and fk constraints that are required for one part of relationship.
  *
  * @param array $joinColumns
  * @param \Doctrine\DBAL\Schema\Table $theJoinTable
  * @param ClassMetadata $class
  * @param \Doctrine\ORM\Mapping\AssociationMapping $mapping
  * @param array $primaryKeyColumns
  * @param array $uniqueConstraints
  */
 private function _gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$uniqueConstraints)
 {
     $localColumns = array();
     $foreignColumns = array();
     $fkOptions = array();
     foreach ($joinColumns as $joinColumn) {
         $columnName = $joinColumn['name'];
         $referencedFieldName = $class->getFieldName($joinColumn['referencedColumnName']);
         if (!$class->hasField($referencedFieldName)) {
             throw new \Doctrine\ORM\ORMException("Column name `" . $joinColumn['referencedColumnName'] . "` referenced for relation from " . $mapping['sourceEntity'] . " towards " . $mapping['targetEntity'] . " does not exist.");
         }
         $primaryKeyColumns[] = $columnName;
         $localColumns[] = $columnName;
         $foreignColumns[] = $joinColumn['referencedColumnName'];
         if (!$theJoinTable->hasColumn($joinColumn['name'])) {
             // Only add the column to the table if it does not exist already.
             // It might exist already if the foreign key is mapped into a regular
             // property as well.
             $fieldMapping = $class->getFieldMapping($referencedFieldName);
             $columnDef = null;
             if (isset($joinColumn['columnDefinition'])) {
                 $columnDef = $joinColumn['columnDefinition'];
             } else {
                 if (isset($fieldMapping['columnDefinition'])) {
                     $columnDef = $fieldMapping['columnDefinition'];
                 }
             }
             $columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef);
             if (isset($joinColumn['nullable'])) {
                 $columnOptions['notnull'] = !$joinColumn['nullable'];
             }
             if ($fieldMapping['type'] == "string") {
                 $columnOptions['length'] = $fieldMapping['length'];
             } else {
                 if ($fieldMapping['type'] == "decimal") {
                     $columnOptions['scale'] = $fieldMapping['scale'];
                     $columnOptions['precision'] = $fieldMapping['precision'];
                 }
             }
             $theJoinTable->addColumn($columnName, $class->getTypeOfColumn($joinColumn['referencedColumnName']), $columnOptions);
         }
         if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
             $uniqueConstraints[] = array('columns' => array($columnName));
         }
         if (isset($joinColumn['onUpdate'])) {
             $fkOptions['onUpdate'] = $joinColumn['onUpdate'];
         }
         if (isset($joinColumn['onDelete'])) {
             $fkOptions['onDelete'] = $joinColumn['onDelete'];
         }
     }
     $theJoinTable->addUnnamedForeignKeyConstraint($class->getQuotedTableName($this->_platform), $localColumns, $foreignColumns, $fkOptions);
 }