Permalink
Browse files

Merge pull request #384 from FabioBatSilva/DDC1885

[DDC-1885] Fix quotes in many to many persisters
  • Loading branch information...
2 parents 6ba205f + 632d13b commit 4bfdcd32f7548c628fc393e36bf37ef8b97a80a8 @beberlei beberlei committed Jul 4, 2012
@@ -858,7 +858,11 @@ private function getManyToManyStatement(array $assoc, $sourceEntity, $offset = n
if ($assoc['isOwningSide']) {
$quotedJoinTable = $this->quoteStrategy->getJoinTableName($assoc, $sourceClass, $this->_platform);
- foreach ($assoc['relationToSourceKeyColumns'] as $relationKeyColumn => $sourceKeyColumn) {
+ foreach ($assoc['joinTable']['joinColumns'] as $joinColumn) {
+ $relationKeyColumn = $joinColumn['name'];
+ $sourceKeyColumn = $joinColumn['referencedColumnName'];
+ $quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $sourceClass, $this->_platform);
+
if ($sourceClass->containsForeignIdentifier) {
$field = $sourceClass->getFieldForColumn($sourceKeyColumn);
$value = $sourceClass->reflFields[$field]->getValue($sourceEntity);
@@ -868,9 +872,9 @@ private function getManyToManyStatement(array $assoc, $sourceEntity, $offset = n
$value = $value[$this->_em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]];
}
- $criteria[$quotedJoinTable . "." . $relationKeyColumn] = $value;
+ $criteria[$quotedJoinTable . "." . $quotedKeyColumn] = $value;
} else if (isset($sourceClass->fieldNames[$sourceKeyColumn])) {
- $criteria[$quotedJoinTable . "." . $relationKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
+ $criteria[$quotedJoinTable . "." . $quotedKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
} else {
throw MappingException::joinColumnMustPointToMappedField(
$sourceClass->name, $sourceKeyColumn
@@ -882,7 +886,11 @@ private function getManyToManyStatement(array $assoc, $sourceEntity, $offset = n
$quotedJoinTable = $this->quoteStrategy->getJoinTableName($owningAssoc, $sourceClass, $this->_platform);
// TRICKY: since the association is inverted source and target are flipped
- foreach ($owningAssoc['relationToTargetKeyColumns'] as $relationKeyColumn => $sourceKeyColumn) {
+ foreach ($owningAssoc['joinTable']['inverseJoinColumns'] as $joinColumn) {
+ $relationKeyColumn = $joinColumn['name'];
+ $sourceKeyColumn = $joinColumn['referencedColumnName'];
+ $quotedKeyColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $sourceClass, $this->_platform);
+
if ($sourceClass->containsForeignIdentifier) {
$field = $sourceClass->getFieldForColumn($sourceKeyColumn);
$value = $sourceClass->reflFields[$field]->getValue($sourceEntity);
@@ -892,9 +900,9 @@ private function getManyToManyStatement(array $assoc, $sourceEntity, $offset = n
$value = $value[$this->_em->getClassMetadata($sourceClass->associationMappings[$field]['targetEntity'])->identifier[0]];
}
- $criteria[$quotedJoinTable . "." . $relationKeyColumn] = $value;
+ $criteria[$quotedJoinTable . "." . $quotedKeyColumn] = $value;
} else if (isset($sourceClass->fieldNames[$sourceKeyColumn])) {
- $criteria[$quotedJoinTable . "." . $relationKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
+ $criteria[$quotedJoinTable . "." . $quotedKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
} else {
throw MappingException::joinColumnMustPointToMappedField(
$sourceClass->name, $sourceKeyColumn
@@ -1144,31 +1152,27 @@ protected function _getSelectColumnAssociationSQL($field, $assoc, ClassMetadata
*/
protected function _getSelectManyToManyJoinSQL(array $manyToMany)
{
- if ($manyToMany['isOwningSide']) {
- $owningAssoc = $manyToMany;
- $joinClauses = $manyToMany['relationToTargetKeyColumns'];
- } else {
- $owningAssoc = $this->_em->getClassMetadata($manyToMany['targetEntity'])->associationMappings[$manyToMany['mappedBy']];
- $joinClauses = $owningAssoc['relationToSourceKeyColumns'];
- }
-
- $joinTableName = $this->quoteStrategy->getJoinTableName($owningAssoc, $this->_class, $this->_platform);
- $joinSql = '';
+ $conditions = array();
+ $association = $manyToMany;
+ $sourceTableAlias = $this->_getSQLTableAlias($this->_class->name);
- foreach ($joinClauses as $joinTableColumn => $sourceColumn) {
- if ($joinSql != '') $joinSql .= ' AND ';
+ if ( ! $manyToMany['isOwningSide']) {
+ $targetEntity = $this->_em->getClassMetadata($manyToMany['targetEntity']);
+ $association = $targetEntity->associationMappings[$manyToMany['mappedBy']];
+ }
- if ($this->_class->containsForeignIdentifier && ! isset($this->_class->fieldNames[$sourceColumn])) {
- $quotedColumn = $sourceColumn; // join columns cannot be quoted
- } else {
- $quotedColumn = $this->quoteStrategy->getColumnName($this->_class->fieldNames[$sourceColumn], $this->_class, $this->_platform);
- }
+ $joinTableName = $this->quoteStrategy->getJoinTableName($association, $this->_class, $this->_platform);
+ $joinColumns = ($manyToMany['isOwningSide'])
+ ? $association['joinTable']['inverseJoinColumns']
+ : $association['joinTable']['joinColumns'];
- $joinSql .= $this->_getSQLTableAlias($this->_class->name) . '.' . $quotedColumn . ' = '
- . $joinTableName . '.' . $joinTableColumn;
+ foreach ($joinColumns as $joinColumn) {
+ $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->_class, $this->_platform);
+ $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->_class, $this->_platform);
+ $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableName . '.' . $quotedSourceColumn;
}
- return ' INNER JOIN ' . $joinTableName . ' ON ' . $joinSql;
+ return ' INNER JOIN ' . $joinTableName . ' ON ' . implode(' AND ', $conditions);
}
/**
@@ -40,11 +40,20 @@ class ManyToManyPersister extends AbstractCollectionPersister
*/
protected function _getDeleteRowSQL(PersistentCollection $coll)
{
+ $columns = array();
$mapping = $coll->getMapping();
$class = $this->_em->getClassMetadata(get_class($coll->getOwner()));
+ foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
+ $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
+ }
+
+ foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) {
+ $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
+ }
+
return 'DELETE FROM ' . $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform)
- . ' WHERE ' . implode(' = ? AND ', $mapping['joinTableColumns']) . ' = ?';
+ . ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?';
}
/**
@@ -76,11 +85,19 @@ protected function _getUpdateRowSQL(PersistentCollection $coll)
*/
protected function _getInsertRowSQL(PersistentCollection $coll)
{
+ $columns = array();
$mapping = $coll->getMapping();
- $columns = $mapping['joinTableColumns'];
$class = $this->_em->getClassMetadata(get_class($coll->getOwner()));
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
+ foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
+ $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
+ }
+
+ foreach ($mapping['joinTable']['inverseJoinColumns'] as $joinColumn) {
+ $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
+ }
+
return 'INSERT INTO ' . $joinTable . ' (' . implode(', ', $columns) . ')'
. ' VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')';
}
@@ -147,12 +164,17 @@ private function _collectJoinTableColumnParameters(PersistentCollection $coll, $
*/
protected function _getDeleteSQL(PersistentCollection $coll)
{
- $class = $this->_em->getClassMetadata(get_class($coll->getOwner()));
+ $columns = array();
$mapping = $coll->getMapping();
+ $class = $this->_em->getClassMetadata(get_class($coll->getOwner()));
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform);
+ foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
+ $columns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
+ }
+
return 'DELETE FROM ' . $joinTable
- . ' WHERE ' . implode(' = ? AND ', array_keys($mapping['relationToSourceKeyColumns'])) . ' = ?';
+ . ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?';
}
/**
@@ -190,41 +212,42 @@ protected function _getDeleteSQLParameters(PersistentCollection $coll)
*/
public function count(PersistentCollection $coll)
{
- $mapping = $filterMapping = $coll->getMapping();
- $class = $this->_em->getClassMetadata($mapping['sourceEntity']);
- $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
+ $conditions = array();
+ $params = array();
+ $mapping = $coll->getMapping();
+ $association = $mapping;
+ $class = $this->_em->getClassMetadata($mapping['sourceEntity']);
+ $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
- if ($mapping['isOwningSide']) {
- $joinColumns = $mapping['relationToSourceKeyColumns'];
- } else {
- $mapping = $this->_em->getClassMetadata($mapping['targetEntity'])->associationMappings[$mapping['mappedBy']];
- $joinColumns = $mapping['relationToTargetKeyColumns'];
+ if ( ! $mapping['isOwningSide']) {
+ $targetEntity = $this->_em->getClassMetadata($mapping['targetEntity']);
+ $association = $targetEntity->associationMappings[$mapping['mappedBy']];
}
- $whereClauses = array();
- $params = array();
-
- foreach ($mapping['joinTableColumns'] as $joinTableColumn) {
- if ( ! isset($joinColumns[$joinTableColumn])) {
- continue;
- }
-
- $whereClauses[] = $joinTableColumn . ' = ?';
-
- $params[] = ($class->containsForeignIdentifier)
- ? $id[$class->getFieldForColumn($joinColumns[$joinTableColumn])]
- : $id[$class->fieldNames[$joinColumns[$joinTableColumn]]];
+ $joinColumns = ( ! $mapping['isOwningSide'])
+ ? $association['joinTable']['inverseJoinColumns']
+ : $association['joinTable']['joinColumns'];
+
+ foreach ($joinColumns as $joinColumn) {
+ $columnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
+ $referencedName = $joinColumn['referencedColumnName'];
+ $conditions[] = $columnName . ' = ?';
+ $params[] = ($class->containsForeignIdentifier)
+ ? $id[$class->getFieldForColumn($referencedName)]
+ : $id[$class->fieldNames[$referencedName]];
}
- list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($filterMapping);
+ $joinTableName = $this->quoteStrategy->getJoinTableName($association, $class, $this->platform);
+ list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($mapping);
+
if ($filterSql) {
- $whereClauses[] = $filterSql;
+ $conditions[] = $filterSql;
}
$sql = 'SELECT COUNT(*)'
- . ' FROM ' . $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform) . ' t'
+ . ' FROM ' . $joinTableName . ' t'
. $joinTargetEntitySQL
- . ' WHERE ' . implode(' AND ', $whereClauses);
+ . ' WHERE ' . implode(' AND ', $conditions);
return $this->_conn->fetchColumn($sql, $params);
}
@@ -34,7 +34,7 @@ class User
public $address;
/**
- * @ManyToMany(targetEntity="Group", inversedBy="users", cascade={"persist"})
+ * @ManyToMany(targetEntity="Group", inversedBy="users", cascade={"all"})
* @JoinTable(name="`quote-users-groups`",
* joinColumns={
* @JoinColumn(
@@ -52,6 +52,25 @@ class User
*/
public $groups;
+ /**
+ * @ManyToMany(targetEntity="Group", inversedBy="users", cascade={"all"}, fetch="EXTRA_LAZY")
+ * @JoinTable(name="`quote-extra-lazy-users-groups`",
+ * joinColumns={
+ * @JoinColumn(
+ * name="`user-id`",
+ * referencedColumnName="`user-id`"
+ * )
+ * },
+ * inverseJoinColumns={
+ * @JoinColumn(
+ * name="`group-id`",
+ * referencedColumnName="`group-id`"
+ * )
+ * }
+ * )
+ */
+ public $extraLazyGroups;
+
public function __construct()
{
$this->phones = new ArrayCollection;
Oops, something went wrong.

0 comments on commit 4bfdcd3

Please sign in to comment.