From 2afe24f51c4515107bd0fdf243ff18204056087b Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Wed, 6 Jun 2012 13:14:20 -0300 Subject: [PATCH] start to work in xxToOne quote --- .gitignore | 5 ++ .../ORM/Mapping/DefaultQuoteStrategy.php | 23 ++++++++- lib/Doctrine/ORM/Mapping/QuoteStrategy.php | 9 ++++ lib/Doctrine/ORM/Tools/SchemaTool.php | 18 ++++--- tests/Doctrine/Tests/Models/Quote/Group.php | 2 + tests/Doctrine/Tests/Models/Quote/User.php | 3 +- .../ORM/Functional/Ticket/DDC1843Test.php | 51 ++++++++++++++----- 7 files changed, 88 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index a91841777..265037d31 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,8 @@ lib/Doctrine/DBAL /.settings/ .buildpath .project +/.AppleDouble/ +/bin/.AppleDouble/ +/lib/.AppleDouble/ +/tests/.AppleDouble/ +/tools/.AppleDouble/ \ No newline at end of file diff --git a/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php b/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php index f1c6fa5d2..9ab835a31 100644 --- a/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php +++ b/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php @@ -64,7 +64,7 @@ class DefaultQuoteStrategy extends QuoteStrategy */ public function getJoinColumnName($columnName, array $association, ClassMetadata $class) { - if( !isset($association['joinColumns'])) { + if( ! isset($association['joinColumns'])) { return $columnName; } @@ -80,6 +80,27 @@ class DefaultQuoteStrategy extends QuoteStrategy return $columnName; } + /** + * {@inheritdoc} + */ + public function getReferencedJoinColumnName($columnName, array $association, ClassMetadata $class) + { + if( ! isset($association['joinColumns'])) { + return $columnName; + } + + foreach ($association['joinColumns'] as $joinColumn) { + if($joinColumn['referencedColumnName'] === $columnName) { + if (isset($joinColumn['quoted'])) { + return $this->platform->quoteIdentifier($columnName); + } + return $columnName; + } + } + + return $columnName; + } + /** * {@inheritdoc} */ diff --git a/lib/Doctrine/ORM/Mapping/QuoteStrategy.php b/lib/Doctrine/ORM/Mapping/QuoteStrategy.php index aacf1c03d..c994b5139 100644 --- a/lib/Doctrine/ORM/Mapping/QuoteStrategy.php +++ b/lib/Doctrine/ORM/Mapping/QuoteStrategy.php @@ -87,6 +87,15 @@ abstract class QuoteStrategy */ abstract public function getJoinColumnName($columnName, array $association, ClassMetadata $class); + /** + * Gets the (possibly quoted) join column name. + * + * @param array $association + * @param ClassMetadata $class + * @return string + */ + abstract public function getReferencedJoinColumnName($columnName, array $association, ClassMetadata $class); + /** * Gets the (possibly quoted) identifier column names for safe use in an SQL statement. * diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 04796e6d3..70e5d3224 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -513,8 +513,10 @@ class SchemaTool $foreignTableName = $this->quoteStrategy->getTableName($class); foreach ($joinColumns as $joinColumn) { - $columnName = $joinColumn['name']; - list($definingClass, $referencedFieldName) = $this->getDefiningClass($class, $joinColumn['referencedColumnName']); + $columnName = $joinColumn['name']; + $referencedColumnName = $joinColumn['referencedColumnName']; + + list($definingClass, $referencedFieldName) = $this->getDefiningClass($class, $referencedColumnName); if (!$definingClass) { throw new \Doctrine\ORM\ORMException( @@ -523,12 +525,14 @@ class SchemaTool ); } - $primaryKeyColumns[] = $columnName; - $localColumns[] = $columnName; - $foreignColumns[] = $joinColumn['referencedColumnName']; $quotedColumnName = $this->quoteStrategy->getJoinColumnName($columnName, $mapping, $class); + $quotedRefColumnName = $this->quoteStrategy->getReferencedJoinColumnName($referencedColumnName, $mapping, $class); - if ( ! $theJoinTable->hasColumn($joinColumn['name'])) { + $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. @@ -556,7 +560,7 @@ class SchemaTool } if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) { - $uniqueConstraints[] = array('columns' => array($columnName)); + $uniqueConstraints[] = array('columns' => array($quotedColumnName)); } if (isset($joinColumn['onDelete'])) { diff --git a/tests/Doctrine/Tests/Models/Quote/Group.php b/tests/Doctrine/Tests/Models/Quote/Group.php index 2117f4b69..c653b6974 100644 --- a/tests/Doctrine/Tests/Models/Quote/Group.php +++ b/tests/Doctrine/Tests/Models/Quote/Group.php @@ -22,6 +22,8 @@ class Group public $name; /** + * @var Group + * * @ManyToOne(targetEntity="Group", cascade={"persist"}) * @JoinColumn(name="`parent-id`", referencedColumnName="`group-id`") */ diff --git a/tests/Doctrine/Tests/Models/Quote/User.php b/tests/Doctrine/Tests/Models/Quote/User.php index 33d1c0bcd..7d485aabe 100644 --- a/tests/Doctrine/Tests/Models/Quote/User.php +++ b/tests/Doctrine/Tests/Models/Quote/User.php @@ -40,7 +40,8 @@ class User * joinColumns={ * @JoinColumn( * name="`user-id`", - * referencedColumnName="`user-id`") + * referencedColumnName="`user-id`" + * ) * }, * inverseJoinColumns={ * @JoinColumn( diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php index 4b28f66e2..9ee846cbd 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php @@ -16,14 +16,16 @@ class DDC1843Test extends \Doctrine\Tests\OrmFunctionalTestCase protected function setUp() { - $this->markTestIncomplete(); parent::setUp(); - $this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); + //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); try { $this->_schemaTool->createSchema(array( - $this->_em->getClassMetadata(self::CLASS_NAME), + //$this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\User'), + $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Group'), + //$this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Phone'), + //$this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Address'), )); } catch(\Exception $e) { $this->fail($e->getMessage()); @@ -70,42 +72,63 @@ class DDC1843Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals($e3Id, $e3->id); $this->assertEquals($e4Id, $e4->id); - return; - $this->assertEquals('Bar 1', $e1->value); - $this->assertEquals('Foo 1', $e2->value); + $this->assertEquals('Parent Bar 1', $e1->name); + $this->assertEquals('Parent Foo 2', $e2->name); + $this->assertEquals('Bar 3', $e3->name); + $this->assertEquals('Foo 4', $e4->name); - $e1->value = 'Bar 2'; - $e2->value = 'Foo 2'; + $e1->name = 'Parent Bar 11'; + $e2->name = 'Parent Foo 22'; + $e3->name = 'Bar 33'; + $e4->name = 'Foo 44'; // Update $this->_em->persist($e1); $this->_em->persist($e2); + $this->_em->persist($e3); + $this->_em->persist($e4); $this->_em->flush(); - $this->assertEquals('Bar 2', $e1->value); - $this->assertEquals('Foo 2', $e2->value); + $this->assertEquals('Parent Bar 11', $e1->name); + $this->assertEquals('Parent Foo 22', $e2->name); + $this->assertEquals('Bar 33', $e3->name); + $this->assertEquals('Foo 44', $e4->name); $this->assertInstanceOf(self::CLASS_NAME, $e1); $this->assertInstanceOf(self::CLASS_NAME, $e2); + $this->assertInstanceOf(self::CLASS_NAME, $e3); + $this->assertInstanceOf(self::CLASS_NAME, $e4); $this->assertEquals($e1Id, $e1->id); $this->assertEquals($e2Id, $e2->id); + $this->assertEquals($e3Id, $e3->id); + $this->assertEquals($e4Id, $e4->id); - $this->assertEquals('Bar 2', $e1->value); - $this->assertEquals('Foo 2', $e2->value); + $this->assertEquals('Parent Bar 11', $e1->name); + $this->assertEquals('Parent Foo 22', $e2->name); + $this->assertEquals('Bar 33', $e3->name); + $this->assertEquals('Foo 44', $e4->name); + + $this->markTestIncomplete(); // Delete $this->_em->remove($e1); $this->_em->remove($e2); + $this->_em->remove($e3); + $this->_em->remove($e4); $this->_em->flush(); - $e1 = $this->_em->find(self::CLASS_NAME, $e1Id); - $e2 = $this->_em->find(self::CLASS_NAME, $e2Id); + $this->assertInstanceOf(self::CLASS_NAME, $e1); + $this->assertInstanceOf(self::CLASS_NAME, $e2); + $this->assertInstanceOf(self::CLASS_NAME, $e3); + $this->assertInstanceOf(self::CLASS_NAME, $e4); $this->assertNull($e1); $this->assertNull($e2); + $this->assertNull($e3); + $this->assertNull($e4); } } \ No newline at end of file