From ddb5a66fd88ffe40dfcd48dc2bd48d0506161157 Mon Sep 17 00:00:00 2001 From: romanb Date: Mon, 7 Dec 2009 16:21:29 +0000 Subject: [PATCH] [2.0][DDC-191][DDC-192] Attempt to fix both. --- lib/Doctrine/DBAL/Schema/Table.php | 6 +- lib/Doctrine/ORM/Tools/SchemaTool.php | 14 ++-- .../Functional/SingleTableInheritanceTest.php | 4 -- .../Tests/ORM/Functional/Ticket/AllTests.php | 2 +- .../ORM/Functional/Ticket/DDC192Test.php | 71 +++++++++++++++++++ 5 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC192Test.php diff --git a/lib/Doctrine/DBAL/Schema/Table.php b/lib/Doctrine/DBAL/Schema/Table.php index 63314e883..1fe81fd20 100644 --- a/lib/Doctrine/DBAL/Schema/Table.php +++ b/lib/Doctrine/DBAL/Schema/Table.php @@ -188,7 +188,7 @@ class Table extends AbstractAsset $columnName = $indexColOptions; } - if (!isset($this->_columns[$columnName])) { + if ( ! $this->hasColumn($columnName)) { throw SchemaException::columnDoesNotExist($columnName); } } @@ -304,7 +304,7 @@ class Table extends AbstractAsset $foreignTableName = $foreignTable->getName(); foreach ($foreignColumnNames AS $columnName) { - if (!$foreignTable->hasColumn($columnName)) { + if ( ! $foreignTable->hasColumn($columnName)) { throw SchemaException::columnDoesNotExist($columnName); } } @@ -313,7 +313,7 @@ class Table extends AbstractAsset } foreach ($localColumnNames AS $columnName) { - if (!$this->hasColumn($columnName)) { + if ( ! $this->hasColumn($columnName)) { throw SchemaException::columnDoesNotExist($columnName); } } diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index ccf1f6f25..ab94a2a00 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -384,6 +384,7 @@ class SchemaTool $fkOptions = array(); foreach ($joinColumns as $joinColumn) { + // Note that this thing might be quoted, i.e. `foo`, [foo], ... $columnName = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform); if (!$class->hasField($class->getFieldName($joinColumn['referencedColumnName']))) { @@ -397,11 +398,16 @@ class SchemaTool $localColumns[] = $columnName; $foreignColumns[] = $joinColumn['referencedColumnName']; - $theJoinTable->createColumn( - $columnName, $class->getTypeOfColumn($joinColumn['referencedColumnName']), array('notnull' => false) - ); + 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. + $theJoinTable->createColumn( + $columnName, $class->getTypeOfColumn($joinColumn['referencedColumnName']), array('notnull' => false) + ); + } - if(isset($joinColumn['unique']) && $joinColumn['unique'] == true) { + if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) { $uniqueConstraints[] = array($columnName); } diff --git a/tests/Doctrine/Tests/ORM/Functional/SingleTableInheritanceTest.php b/tests/Doctrine/Tests/ORM/Functional/SingleTableInheritanceTest.php index 6f7292bb6..fc6a408f6 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SingleTableInheritanceTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SingleTableInheritanceTest.php @@ -183,10 +183,6 @@ class SingleTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals('related to parent!', $related->getData()); } - /*public function testPolymorphicQueryWithJoin() - { - - }*/ } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/AllTests.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/AllTests.php index 7998e89c7..5820a273f 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/AllTests.php @@ -19,7 +19,7 @@ class AllTests { $suite = new \Doctrine\Tests\OrmFunctionalTestSuite('Doctrine Orm Ticket Tests'); - $tests = glob(__DIR__ . '/Ticket*Test.php'); + $tests = glob(__DIR__ . '/*Test.php'); foreach ($tests as $test) { $info = pathinfo($test); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\Ticket\\' . $info['filename']); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC192Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC192Test.php new file mode 100644 index 000000000..07c8d1317 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC192Test.php @@ -0,0 +1,71 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC192User'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC192Phonenumber') + )); + } +} + + +/** + * @Entity @Table(name="ddc192_users") + */ +class DDC192User +{ + /** + * @Id @Column(name="id", type="integer") + * @GeneratedValue(strategy="AUTO") + */ + public $id; + + /** + * @Column(name="name", type="string") + */ + public $name; +} + + +/** + * @Entity @Table(name="ddc192_phonenumbers") + */ +class DDC192Phonenumber +{ + /** + * @Id @Column(name="phone", type="string", length=40) + */ + protected $phone; + + /** + * @Id @Column(name="userId", type="integer") + */ + protected $userId; + + /** + * @Id + * @ManyToOne(targetEntity="DDC192User") + * @JoinColumn(name="userId", referencedColumnName="id") + */ + protected $User; // Id on this docblock is ignored! + + + public function setPhone($value) { $this->phone = $value; } + + public function getPhone() { return $this->phone; } + + public function setUser(User $user) + { + $this->User = $user; + $this->userId = $user->getId(); // TODO: Remove once ManyToOne supports Id annotation + } + + public function getUser() { return $this->User; } +} \ No newline at end of file