예제 #1
1
 /**
  * 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 array $mapping
  * @param array $primaryKeyColumns
  * @param array $uniqueConstraints
  */
 private function _gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$uniqueConstraints)
 {
     $localColumns = array();
     $foreignColumns = array();
     $fkOptions = array();
     $foreignTableName = $this->quoteStrategy->getTableName($class, $this->platform);
     foreach ($joinColumns as $joinColumn) {
         list($definingClass, $referencedFieldName) = $this->getDefiningClass($class, $joinColumn['referencedColumnName']);
         if (!$definingClass) {
             throw new \Doctrine\ORM\ORMException("Column name `" . $joinColumn['referencedColumnName'] . "` referenced for relation from " . $mapping['sourceEntity'] . " towards " . $mapping['targetEntity'] . " does not exist.");
         }
         $quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
         $quotedRefColumnName = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $class, $this->platform);
         $primaryKeyColumns[] = $quotedColumnName;
         $localColumns[] = $quotedColumnName;
         $foreignColumns[] = $quotedRefColumnName;
         if (!$theJoinTable->hasColumn($quotedColumnName)) {
             // 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 = $definingClass->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 (isset($fieldMapping['options'])) {
                 $columnOptions['options'] = $fieldMapping['options'];
             }
             if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) {
                 $columnOptions['length'] = $fieldMapping['length'];
             } else {
                 if ($fieldMapping['type'] == "decimal") {
                     $columnOptions['scale'] = $fieldMapping['scale'];
                     $columnOptions['precision'] = $fieldMapping['precision'];
                 }
             }
             $theJoinTable->addColumn($quotedColumnName, $fieldMapping['type'], $columnOptions);
         }
         if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
             $uniqueConstraints[] = array('columns' => array($quotedColumnName));
         }
         if (isset($joinColumn['onDelete'])) {
             $fkOptions['onDelete'] = $joinColumn['onDelete'];
         }
     }
     $theJoinTable->addUnnamedForeignKeyConstraint($foreignTableName, $localColumns, $foreignColumns, $fkOptions);
 }
예제 #2
0
 /**
  * Apply the ACL constraints to the specified query builder, using the permission definition
  *
  * @param QueryBuilder         $queryBuilder  The query builder
  * @param PermissionDefinition $permissionDef The permission definition
  *
  * @return Query
  */
 public function apply(QueryBuilder $queryBuilder, PermissionDefinition $permissionDef)
 {
     $whereQueryParts = $queryBuilder->getDQLPart('where');
     if (empty($whereQueryParts)) {
         $queryBuilder->where('1 = 1');
         // this will help in cases where no where query is specified
     }
     $query = $this->cloneQuery($queryBuilder->getQuery());
     $builder = new MaskBuilder();
     foreach ($permissionDef->getPermissions() as $permission) {
         $mask = constant(get_class($builder) . '::MASK_' . strtoupper($permission));
         $builder->add($mask);
     }
     $query->setHint('acl.mask', $builder->get());
     $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Kunstmaan\\AdminBundle\\Helper\\Security\\Acl\\AclWalker');
     $rootEntity = $permissionDef->getEntity();
     $rootAlias = $permissionDef->getAlias();
     // If either alias or entity was not specified - use default from QueryBuilder
     if (empty($rootEntity) || empty($rootAlias)) {
         $rootEntities = $queryBuilder->getRootEntities();
         $rootAliases = $queryBuilder->getRootAliases();
         $rootEntity = $rootEntities[0];
         $rootAlias = $rootAliases[0];
     }
     $query->setHint('acl.root.entity', $rootEntity);
     $query->setHint('acl.extra.query', $this->getPermittedAclIdsSQLForUser($query));
     $classMeta = $this->em->getClassMetadata($rootEntity);
     $entityRootTableName = $this->quoteStrategy->getTableName($classMeta, $this->em->getConnection()->getDatabasePlatform());
     $query->setHint('acl.entityRootTableName', $entityRootTableName);
     $query->setHint('acl.entityRootTableDqlAlias', $rootAlias);
     return $query;
 }
예제 #3
0
 public function getEntityHistory($className, $id)
 {
     if (!$this->metadataFactory->isAudited($className)) {
         throw new NotAuditedException($className);
     }
     /** @var ClassMetadataInfo|ClassMetadata $class */
     $class = $this->em->getClassMetadata($className);
     $tableName = $this->config->getTableName($class);
     if (!is_array($id)) {
         $id = array($class->identifier[0] => $id);
     }
     $whereId = array();
     foreach ($class->identifier as $idField) {
         if (isset($class->fieldMappings[$idField])) {
             $columnName = $class->fieldMappings[$idField]['columnName'];
         } else {
             if (isset($class->associationMappings[$idField])) {
                 $columnName = $class->associationMappings[$idField]['joinColumns'][0];
             } else {
                 continue;
             }
         }
         $whereId[] = "{$columnName} = ?";
     }
     $whereSQL = implode(' AND ', $whereId);
     $columnList = array($this->config->getRevisionFieldName());
     $columnMap = array();
     foreach ($class->fieldNames as $columnName => $field) {
         $type = Type::getType($class->fieldMappings[$field]['type']);
         $columnList[] = $type->convertToPHPValueSQL($this->quoteStrategy->getColumnName($field, $class, $this->platform), $this->platform) . ' AS ' . $this->platform->quoteSingleIdentifier($field);
         $columnMap[$field] = $this->platform->getSQLResultCasing($columnName);
     }
     foreach ($class->associationMappings as $assoc) {
         if (($assoc['type'] & ClassMetadata::TO_ONE) == 0 || !$assoc['isOwningSide']) {
             continue;
         }
         foreach ($assoc['targetToSourceKeyColumns'] as $sourceCol) {
             $columnList[] = $sourceCol;
             $columnMap[$sourceCol] = $this->platform->getSQLResultCasing($sourceCol);
         }
     }
     $values = array_values($id);
     $query = "SELECT " . implode(', ', $columnList) . " FROM " . $tableName . " e WHERE " . $whereSQL . " ORDER BY e.rev DESC";
     $stmt = $this->em->getConnection()->executeQuery($query, $values);
     $result = array();
     while ($row = $stmt->fetch(Query::HYDRATE_ARRAY)) {
         $rev = $row[$this->config->getRevisionFieldName()];
         unset($row[$this->config->getRevisionFieldName()]);
         $result[] = $this->createEntity($class->name, $columnMap, $row, $rev);
     }
     return $result;
 }
 private function getUniqueAndRequiredAssociations(ClassMetadata $meta, $entity)
 {
     $associations = array();
     foreach ($meta->getAssociationNames() as $associationName) {
         $mapping = $meta->getAssociationMapping($associationName);
         if (!empty($mapping['id']) && $meta->usesIdGenerator()) {
             // autogenerated id
             continue;
         }
         if (!($mapping['type'] & ClassMetadata::TO_ONE)) {
             // is not to one relation
             continue;
         }
         if (empty($mapping['isOwningSide'])) {
             // is not owning side
             continue;
         }
         foreach ($mapping['joinColumns'] as $joinColumn) {
             if (!empty($joinColumn['nullable']) && empty($joinColumn['unique'])) {
                 // is nullable and is not unique
                 continue;
             }
             $targetColumn = $joinColumn['referencedColumnName'];
             $targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
             $newVal = $meta->getFieldValue($entity, $associationName);
             if ($newVal !== NULL) {
                 $newValId = $this->uow->getEntityIdentifier($newVal);
             }
             switch (TRUE) {
                 case $newVal === NULL:
                     $value = NULL;
                     break;
                 case $targetClass->containsForeignIdentifier:
                     $value = $newValId[$targetClass->getFieldForColumn($targetColumn)];
                     break;
                 default:
                     $value = $newValId[$targetClass->fieldNames[$targetColumn]];
                     break;
             }
             $sourceColumn = $joinColumn['name'];
             $quotedColumn = $this->quotes->getJoinColumnName($joinColumn, $meta, $this->platform);
             $associations[$sourceColumn]['value'] = $value;
             $associations[$sourceColumn]['quotedColumn'] = $quotedColumn;
             $associations[$sourceColumn]['type'] = $targetClass->getTypeOfColumn($targetColumn);
         }
     }
     return $associations;
 }
 /**
  * Generates a new order by clause that works in the scope of a select query wrapping the original
  *
  * @param OrderByClause $orderByClause
  * @return array
  */
 private function rebuildOrderByClauseForOuterScope(OrderByClause $orderByClause)
 {
     $dqlAliasToSqlTableAliasMap = $searchPatterns = $replacements = $dqlAliasToClassMap = $selectListAdditions = $orderByItems = [];
     // Generate DQL alias -> SQL table alias mapping
     foreach (array_keys($this->rsm->aliasMap) as $dqlAlias) {
         $dqlAliasToClassMap[$dqlAlias] = $class = $this->queryComponents[$dqlAlias]['metadata'];
         $dqlAliasToSqlTableAliasMap[$dqlAlias] = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
     }
     // Pattern to find table path expressions in the order by clause
     $fieldSearchPattern = '/(?<![a-z0-9_])%s\\.%s(?![a-z0-9_])/i';
     // Generate search patterns for each field's path expression in the order by clause
     foreach ($this->rsm->fieldMappings as $fieldAlias => $fieldName) {
         $dqlAliasForFieldAlias = $this->rsm->columnOwnerMap[$fieldAlias];
         $class = $dqlAliasToClassMap[$dqlAliasForFieldAlias];
         // If the field is from a joined child table, we won't be ordering
         // on it.
         if (!isset($class->fieldMappings[$fieldName])) {
             continue;
         }
         $fieldMapping = $class->fieldMappings[$fieldName];
         // Get the proper column name as will appear in the select list
         $columnName = $this->quoteStrategy->getColumnName($fieldName, $dqlAliasToClassMap[$dqlAliasForFieldAlias], $this->em->getConnection()->getDatabasePlatform());
         // Get the SQL table alias for the entity and field
         $sqlTableAliasForFieldAlias = $dqlAliasToSqlTableAliasMap[$dqlAliasForFieldAlias];
         if (isset($fieldMapping['declared']) && $fieldMapping['declared'] !== $class->name) {
             // Field was declared in a parent class, so we need to get the proper SQL table alias
             // for the joined parent table.
             $otherClassMetadata = $this->em->getClassMetadata($fieldMapping['declared']);
             if (!$otherClassMetadata->isMappedSuperclass) {
                 $sqlTableAliasForFieldAlias = $this->getSQLTableAlias($otherClassMetadata->getTableName(), $dqlAliasForFieldAlias);
             }
         }
         // Compose search/replace patterns
         $searchPatterns[] = sprintf($fieldSearchPattern, $sqlTableAliasForFieldAlias, $columnName);
         $replacements[] = $fieldAlias;
     }
     foreach ($orderByClause->orderByItems as $orderByItem) {
         // Walk order by item to get string representation of it
         $orderByItemString = $this->walkOrderByItem($orderByItem);
         // Replace path expressions in the order by clause with their column alias
         $orderByItemString = preg_replace($searchPatterns, $replacements, $orderByItemString);
         $orderByItems[] = $orderByItemString;
     }
     return $orderByItems;
 }
 /**
  * Generates a new order by clause that works in the scope of a select query wrapping the original
  *
  * @param OrderByClause $orderByClause
  * @return array
  */
 private function rebuildOrderByClauseForOuterScope(OrderByClause $orderByClause)
 {
     $dqlAliasToSqlTableAliasMap = $searchPatterns = $replacements = $dqlAliasToClassMap = $selectListAdditions = $orderByItems = [];
     // Generate DQL alias -> SQL table alias mapping
     foreach (array_keys($this->rsm->aliasMap) as $dqlAlias) {
         $dqlAliasToClassMap[$dqlAlias] = $class = $this->queryComponents[$dqlAlias]['metadata'];
         $dqlAliasToSqlTableAliasMap[$dqlAlias] = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
     }
     // Pattern to find table path expressions in the order by clause
     $fieldSearchPattern = '/(?<![a-z0-9_])%s\\.%s(?![a-z0-9_])/i';
     // Generate search patterns for each field's path expression in the order by clause
     foreach ($this->rsm->fieldMappings as $fieldAlias => $columnName) {
         $dqlAliasForFieldAlias = $this->rsm->columnOwnerMap[$fieldAlias];
         $columnName = $this->quoteStrategy->getColumnName($columnName, $dqlAliasToClassMap[$dqlAliasForFieldAlias], $this->em->getConnection()->getDatabasePlatform());
         $sqlTableAliasForFieldAlias = $dqlAliasToSqlTableAliasMap[$dqlAliasForFieldAlias];
         $searchPatterns[] = sprintf($fieldSearchPattern, $sqlTableAliasForFieldAlias, $columnName);
         $replacements[] = $fieldAlias;
     }
     $complexAddedOrderByAliases = 0;
     foreach ($orderByClause->orderByItems as $orderByItem) {
         // Walk order by item to get string representation of it
         $orderByItemString = $this->walkOrderByItem($orderByItem);
         // Replace path expressions in the order by clause with their column alias
         $orderByItemString = preg_replace($searchPatterns, $replacements, $orderByItemString);
         // The order by items are not required to be in the select list on Oracle and PostgreSQL, but
         // for the sake of simplicity, order by items will be included in the select list on all platforms.
         // This doesn't impact functionality.
         $selectListAddition = trim(preg_replace('/([^ ]+) (?:asc|desc)/i', '$1', $orderByItemString));
         // If the expression is an arithmetic expression, we need to create an alias for it.
         if ($orderByItem->expression instanceof ArithmeticTerm) {
             $orderByAlias = "ordr_" . $complexAddedOrderByAliases++;
             $orderByItemString = $orderByAlias . " " . $orderByItem->type;
             $selectListAddition .= " AS {$orderByAlias}";
         }
         $selectListAdditions[] = $selectListAddition;
         $orderByItems[] = $orderByItemString;
     }
     return array($selectListAdditions, $orderByItems);
 }
 /**
  * @param ClassMetadata $class
  *
  * @return string
  * @throws \Doctrine\DBAL\DBALException
  */
 private function getInsertRevisionSQL($class)
 {
     if (!isset($this->insertRevisionSQL[$class->name])) {
         $placeholders = array('?', '?');
         $tableName = $this->config->getTableName($class);
         $sql = "INSERT INTO " . $tableName . " (" . $this->config->getRevisionFieldName() . ", " . $this->config->getRevisionTypeFieldName();
         $fields = array();
         foreach ($class->associationMappings as $field => $assoc) {
             if ($class->isInheritanceTypeJoined() && $class->isInheritedAssociation($field)) {
                 continue;
             }
             if (($assoc['type'] & ClassMetadata::TO_ONE) > 0 && $assoc['isOwningSide']) {
                 foreach ($assoc['targetToSourceKeyColumns'] as $sourceCol) {
                     $fields[$sourceCol] = true;
                     $sql .= ', ' . $sourceCol;
                     $placeholders[] = '?';
                 }
             }
         }
         foreach ($class->fieldNames as $field) {
             if (array_key_exists($field, $fields)) {
                 continue;
             }
             if ($class->isInheritanceTypeJoined() && $class->isInheritedField($field) && !$class->isIdentifier($field)) {
                 continue;
             }
             $type = Type::getType($class->fieldMappings[$field]['type']);
             $placeholders[] = !empty($class->fieldMappings[$field]['requireSQLConversion']) ? $type->convertToDatabaseValueSQL('?', $this->platform) : '?';
             $sql .= ', ' . $this->quoteStrategy->getColumnName($field, $class, $this->platform);
         }
         if ($class->isInheritanceTypeJoined() && $class->rootEntityName == $class->name || $class->isInheritanceTypeSingleTable()) {
             $sql .= ', ' . $class->discriminatorColumn['name'];
             $placeholders[] = '?';
         }
         $sql .= ") VALUES (" . implode(", ", $placeholders) . ")";
         $this->insertRevisionSQL[$class->name] = $sql;
     }
     return $this->insertRevisionSQL[$class->name];
 }
예제 #8
0
 /**
  * {@inheritdoc}
  */
 public function walkCollectionMemberExpression($collMemberExpr)
 {
     $sql = $collMemberExpr->not ? 'NOT ' : '';
     $sql .= 'EXISTS (SELECT 1 FROM ';
     $entityExpr = $collMemberExpr->entityExpression;
     $collPathExpr = $collMemberExpr->collectionValuedPathExpression;
     $fieldName = $collPathExpr->field;
     $dqlAlias = $collPathExpr->identificationVariable;
     $class = $this->queryComponents[$dqlAlias]['metadata'];
     switch (true) {
         // InputParameter
         case $entityExpr instanceof AST\InputParameter:
             $dqlParamKey = $entityExpr->name;
             $entitySql = '?';
             break;
             // SingleValuedAssociationPathExpression | IdentificationVariable
         // SingleValuedAssociationPathExpression | IdentificationVariable
         case $entityExpr instanceof AST\PathExpression:
             $entitySql = $this->walkPathExpression($entityExpr);
             break;
         default:
             throw new \BadMethodCallException("Not implemented");
     }
     $assoc = $class->associationMappings[$fieldName];
     if ($assoc['type'] == ClassMetadata::ONE_TO_MANY) {
         $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
         $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName());
         $sourceTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
         $sql .= $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' WHERE ';
         $owningAssoc = $targetClass->associationMappings[$assoc['mappedBy']];
         $sqlParts = array();
         foreach ($owningAssoc['targetToSourceKeyColumns'] as $targetColumn => $sourceColumn) {
             $targetColumn = $this->quoteStrategy->getColumnName($class->fieldNames[$targetColumn], $class, $this->platform);
             $sqlParts[] = $sourceTableAlias . '.' . $targetColumn . ' = ' . $targetTableAlias . '.' . $sourceColumn;
         }
         foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass, $this->platform) as $targetColumnName) {
             if (isset($dqlParamKey)) {
                 $this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++);
             }
             $sqlParts[] = $targetTableAlias . '.' . $targetColumnName . ' = ' . $entitySql;
         }
         $sql .= implode(' AND ', $sqlParts);
     } else {
         // many-to-many
         $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
         $owningAssoc = $assoc['isOwningSide'] ? $assoc : $targetClass->associationMappings[$assoc['mappedBy']];
         $joinTable = $owningAssoc['joinTable'];
         // SQL table aliases
         $joinTableAlias = $this->getSQLTableAlias($joinTable['name']);
         $targetTableAlias = $this->getSQLTableAlias($targetClass->getTableName());
         $sourceTableAlias = $this->getSQLTableAlias($class->getTableName(), $dqlAlias);
         // join to target table
         $sql .= $this->quoteStrategy->getJoinTableName($owningAssoc, $targetClass, $this->platform) . ' ' . $joinTableAlias . ' INNER JOIN ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' ON ';
         // join conditions
         $joinColumns = $assoc['isOwningSide'] ? $joinTable['inverseJoinColumns'] : $joinTable['joinColumns'];
         $joinSqlParts = array();
         foreach ($joinColumns as $joinColumn) {
             $targetColumn = $this->quoteStrategy->getColumnName($targetClass->fieldNames[$joinColumn['referencedColumnName']], $targetClass, $this->platform);
             $joinSqlParts[] = $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $targetTableAlias . '.' . $targetColumn;
         }
         $sql .= implode(' AND ', $joinSqlParts);
         $sql .= ' WHERE ';
         $joinColumns = $assoc['isOwningSide'] ? $joinTable['joinColumns'] : $joinTable['inverseJoinColumns'];
         $sqlParts = array();
         foreach ($joinColumns as $joinColumn) {
             $targetColumn = $this->quoteStrategy->getColumnName($class->fieldNames[$joinColumn['referencedColumnName']], $class, $this->platform);
             $sqlParts[] = $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $sourceTableAlias . '.' . $targetColumn;
         }
         foreach ($this->quoteStrategy->getIdentifierColumnNames($targetClass, $this->platform) as $targetColumnName) {
             if (isset($dqlParamKey)) {
                 $this->parserResult->addParameterMapping($dqlParamKey, $this->sqlParamIndex++);
             }
             $sqlParts[] = $targetTableAlias . '.' . $targetColumnName . ' = ' . $entitySql;
         }
         $sql .= implode(' AND ', $sqlParts);
     }
     return $sql . ')';
 }
예제 #9
0
 /**
  * Gets an SQL column alias for a column name.
  *
  * @param string $columnName
  *
  * @return string
  */
 public function getSQLColumnAlias($columnName)
 {
     return $this->quoteStrategy->getColumnAlias($columnName, $this->sqlAliasCounter++, $this->platform);
 }
예제 #10
0
 /**
  * Gathers columns and fk constraints that are required for one part of relationship.
  *
  * @param array         $joinColumns
  * @param Table         $theJoinTable
  * @param ClassMetadata $class
  * @param array         $mapping
  * @param array         $primaryKeyColumns
  * @param array         $addedFks
  * @param array         $blacklistedFks
  *
  * @return void
  *
  * @throws \Doctrine\ORM\ORMException
  */
 private function gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$addedFks, &$blacklistedFks)
 {
     $localColumns = array();
     $foreignColumns = array();
     $fkOptions = array();
     $foreignTableName = $this->quoteStrategy->getTableName($class, $this->platform);
     $uniqueConstraints = array();
     foreach ($joinColumns as $joinColumn) {
         list($definingClass, $referencedFieldName) = $this->getDefiningClass($class, $joinColumn['referencedColumnName']);
         if (!$definingClass) {
             throw new \Doctrine\ORM\ORMException("Column name `" . $joinColumn['referencedColumnName'] . "` referenced for relation from " . $mapping['sourceEntity'] . " towards " . $mapping['targetEntity'] . " does not exist.");
         }
         $quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
         $quotedRefColumnName = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $class, $this->platform);
         $primaryKeyColumns[] = $quotedColumnName;
         $localColumns[] = $quotedColumnName;
         $foreignColumns[] = $quotedRefColumnName;
         if (!$theJoinTable->hasColumn($quotedColumnName)) {
             // 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 = $definingClass->getFieldMapping($referencedFieldName);
             $columnDef = null;
             if (isset($joinColumn['columnDefinition'])) {
                 $columnDef = $joinColumn['columnDefinition'];
             } elseif (isset($fieldMapping['columnDefinition'])) {
                 $columnDef = $fieldMapping['columnDefinition'];
             }
             $columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef);
             if (isset($joinColumn['nullable'])) {
                 $columnOptions['notnull'] = !$joinColumn['nullable'];
             }
             if (isset($fieldMapping['options'])) {
                 $columnOptions['options'] = $fieldMapping['options'];
             }
             if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) {
                 $columnOptions['length'] = $fieldMapping['length'];
             } elseif ($fieldMapping['type'] == "decimal") {
                 $columnOptions['scale'] = $fieldMapping['scale'];
                 $columnOptions['precision'] = $fieldMapping['precision'];
             }
             $theJoinTable->addColumn($quotedColumnName, $fieldMapping['type'], $columnOptions);
         }
         if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
             $uniqueConstraints[] = array('columns' => array($quotedColumnName));
         }
         if (isset($joinColumn['onDelete'])) {
             $fkOptions['onDelete'] = $joinColumn['onDelete'];
         }
     }
     // Prefer unique constraints over implicit simple indexes created for foreign keys.
     // Also avoids index duplication.
     foreach ($uniqueConstraints as $indexName => $unique) {
         $theJoinTable->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
     }
     $compositeName = $theJoinTable->getName() . '.' . implode('', $localColumns);
     if (isset($addedFks[$compositeName]) && ($foreignTableName != $addedFks[$compositeName]['foreignTableName'] || 0 < count(array_diff($foreignColumns, $addedFks[$compositeName]['foreignColumns'])))) {
         foreach ($theJoinTable->getForeignKeys() as $fkName => $key) {
             if (0 === count(array_diff($key->getLocalColumns(), $localColumns)) && ($key->getForeignTableName() != $foreignTableName || 0 < count(array_diff($key->getForeignColumns(), $foreignColumns)))) {
                 $theJoinTable->removeForeignKey($fkName);
                 break;
             }
         }
         $blacklistedFks[$compositeName] = true;
     } elseif (!isset($blacklistedFks[$compositeName])) {
         $addedFks[$compositeName] = array('foreignTableName' => $foreignTableName, 'foreignColumns' => $foreignColumns);
         $theJoinTable->addUnnamedForeignKeyConstraint($foreignTableName, $localColumns, $foreignColumns, $fkOptions);
     }
 }
 /**
  * @param DoctrineSqlFilter $sqlFilter
  * @param QuoteStrategy $quoteStrategy
  * @param ClassMetadata $targetEntity
  * @param string $targetTableAlias
  * @param string $targetEntityPropertyName
  * @return string
  * @throws InvalidQueryRewritingConstraintException
  * @throws \Exception
  */
 protected function getSqlForManyToOneAndOneToOneRelationsWithPropertyPath(DoctrineSqlFilter $sqlFilter, QuoteStrategy $quoteStrategy, ClassMetadata $targetEntity, $targetTableAlias, $targetEntityPropertyName)
 {
     $subselectQuery = $this->getSubselectQuery($targetEntity, $targetEntityPropertyName);
     $associationMapping = $targetEntity->getAssociationMapping($targetEntityPropertyName);
     $subselectConstraintQueries = array();
     foreach ($associationMapping['joinColumns'] as $joinColumn) {
         $rootAliases = $subselectQuery->getQueryBuilder()->getRootAliases();
         $subselectQuery->getQueryBuilder()->select($rootAliases[0] . '.' . $targetEntity->getFieldForColumn($joinColumn['referencedColumnName']));
         $subselectSql = $subselectQuery->getSql();
         foreach ($subselectQuery->getParameters() as $parameter) {
             $parameterValue = $parameter->getValue();
             if (is_object($parameterValue)) {
                 $parameterValue = $this->persistenceManager->getIdentifierByObject($parameter->getValue());
             }
             $subselectSql = preg_replace('/\\?/', $this->entityManager->getConnection()->quote($parameterValue, $parameter->getType()), $subselectSql, 1);
         }
         $quotedColumnName = $quoteStrategy->getJoinColumnName($joinColumn, $targetEntity, $this->entityManager->getConnection()->getDatabasePlatform());
         $subselectIdentifier = 'subselect' . md5($subselectSql);
         $subselectConstraintQueries[] = $targetTableAlias . '.' . $quotedColumnName . ' IN (SELECT ' . $subselectIdentifier . '.' . $joinColumn['referencedColumnName'] . '0 FROM (' . $subselectSql . ') AS ' . $subselectIdentifier . ' ) ';
     }
     return ' (' . implode(' ) AND ( ', $subselectConstraintQueries) . ') ';
 }
 /**
  * Gets the FROM and optionally JOIN conditions to lock the entity managed by this persister.
  *
  * @param integer $lockMode One of the Doctrine\DBAL\LockMode::* constants.
  *
  * @return string
  */
 protected function getLockTablesSql($lockMode)
 {
     return $this->platform->appendLockHint('FROM ' . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' . $this->getSQLTableAlias($this->class->name), $lockMode);
 }