fix DDC-1885 in persisters
This commit is contained in:
parent
1023af6a1f
commit
076663fe3a
4 changed files with 136 additions and 31 deletions
|
@ -846,7 +846,11 @@ class BasicEntityPersister
|
|||
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);
|
||||
|
@ -856,9 +860,9 @@ class BasicEntityPersister
|
|||
$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
|
||||
|
@ -870,7 +874,11 @@ class BasicEntityPersister
|
|||
$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);
|
||||
|
@ -880,9 +888,9 @@ class BasicEntityPersister
|
|||
$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
|
||||
|
@ -1132,31 +1140,28 @@ class BasicEntityPersister
|
|||
*/
|
||||
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'];
|
||||
|
||||
$conditions = array();
|
||||
$sourceTableAlias = $this->_getSQLTableAlias($this->_class->name);
|
||||
|
||||
$association = ($manyToMany['isOwningSide'])
|
||||
? $manyToMany
|
||||
: $this->_em->getClassMetadata($manyToMany['targetEntity'])
|
||||
->associationMappings[$manyToMany['mappedBy']];
|
||||
|
||||
$relationColumns = ($manyToMany['isOwningSide'])
|
||||
? $association['joinTable']['inverseJoinColumns']
|
||||
: $association['joinTable']['joinColumns'];
|
||||
|
||||
$joinTableName = $this->quoteStrategy->getJoinTableName($association, $this->_class, $this->_platform);
|
||||
foreach ($relationColumns 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;
|
||||
}
|
||||
|
||||
$joinTableName = $this->quoteStrategy->getJoinTableName($owningAssoc, $this->_class, $this->_platform);
|
||||
$joinSql = '';
|
||||
|
||||
foreach ($joinClauses as $joinTableColumn => $sourceColumn) {
|
||||
if ($joinSql != '') $joinSql .= ' AND ';
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
$joinSql .= $this->_getSQLTableAlias($this->_class->name) . '.' . $quotedColumn . ' = '
|
||||
. $joinTableName . '.' . $joinTableColumn;
|
||||
}
|
||||
|
||||
return ' INNER JOIN ' . $joinTableName . ' ON ' . $joinSql;
|
||||
return ' INNER JOIN ' . $joinTableName . ' ON ' . implode(' AND ', $conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -76,11 +76,19 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
|||
*/
|
||||
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), '?')) . ')';
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
92
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1885Test.php
Normal file
92
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1885Test.php
Normal file
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
namespace Doctrine\Tests\ORM\Functional\Ticket;
|
||||
|
||||
use Doctrine\Tests\Models\Quote\Group;
|
||||
use Doctrine\Tests\Models\Quote\User;
|
||||
|
||||
require_once __DIR__ . '/../../../TestInit.php';
|
||||
|
||||
/**
|
||||
* @group DDC-1845
|
||||
* @group DDC-1885
|
||||
*/
|
||||
class DDC1885Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
{
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
try {
|
||||
$this->_schemaTool->createSchema(array(
|
||||
$this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\User'),
|
||||
$this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Group'),
|
||||
$this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Address'),
|
||||
));
|
||||
} catch(\Exception $e) {
|
||||
}
|
||||
}
|
||||
|
||||
public function testCreateRetreaveUpdateDelete()
|
||||
{
|
||||
|
||||
$g1 = new Group('G 1');
|
||||
$g2 = new Group('G 2');
|
||||
$user = new User();
|
||||
|
||||
$user->name = "FabioBatSilva";
|
||||
$user->email = "fabio.bat.silva@gmail.com";
|
||||
$user->groups[] = $g1;
|
||||
$user->groups[] = $g2;
|
||||
|
||||
// Create
|
||||
$this->_em->persist($user);
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$u1Id = $user->id;
|
||||
$g1Id = $g1->id;
|
||||
$g2Id = $g2->id;
|
||||
|
||||
// Retreave
|
||||
$user = $this->_em->find('Doctrine\Tests\Models\Quote\User', $u1Id);
|
||||
|
||||
$this->assertInstanceOf('Doctrine\Tests\Models\Quote\User', $user);
|
||||
$this->assertEquals('FabioBatSilva', $user->name);
|
||||
$this->assertEquals($u1Id, $user->id);
|
||||
|
||||
$this->assertCount(2, $user->groups);
|
||||
|
||||
$g1 = $user->getGroups()->get(0);
|
||||
$g2 = $user->getGroups()->get(1);
|
||||
|
||||
$this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $g1);
|
||||
$this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $g2);
|
||||
|
||||
$g1->name = 'Bar 11';
|
||||
$g2->name = 'Foo 22';
|
||||
|
||||
// Update
|
||||
$this->_em->persist($user);
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$user = $this->_em->find('Doctrine\Tests\Models\Quote\User', $u1Id);
|
||||
|
||||
$this->assertInstanceOf('Doctrine\Tests\Models\Quote\User', $user);
|
||||
$this->assertEquals('FabioBatSilva', $user->name);
|
||||
$this->assertEquals($u1Id, $user->id);
|
||||
|
||||
// Delete
|
||||
$this->_em->remove($user);
|
||||
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$this->assertNull($this->_em->find('Doctrine\Tests\Models\Quote\User', $u1Id));
|
||||
$this->assertNull($this->_em->find('Doctrine\Tests\Models\Quote\Group', $g1Id));
|
||||
$this->assertNull($this->_em->find('Doctrine\Tests\Models\Quote\Group', $g2Id));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue