From cd806b83db61c9671bd30c1beeb3c02e11e3b32f Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Mon, 11 Jun 2012 17:41:00 -0300 Subject: [PATCH] fix DDC-142 persist OneToOne --- .../ORM/Persisters/BasicEntityPersister.php | 19 +++++- tests/Doctrine/Tests/Models/Quote/Address.php | 24 +++++++ tests/Doctrine/Tests/Models/Quote/User.php | 25 +++++++- .../ORM/Functional/Ticket/DDC142Test.php | 63 +++++++++++++++++++ .../ORM/Functional/Ticket/DDC1843Test.php | 46 +++++++------- 5 files changed, 149 insertions(+), 28 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC142Test.php diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index da938d39f..01e7b6106 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -132,6 +132,15 @@ class BasicEntityPersister */ protected $_columnTypes = array(); + /** + * The map of quoted column names. + * + * @var array + * @see _prepareInsertData($entity) + * @see _prepareUpdateData($entity) + */ + protected $quotedColumns = array(); + /** * The INSERT SQL statement used for entities handled by this persister. * This SQL is only generated once per request, if at all. @@ -358,6 +367,8 @@ class BasicEntityPersister $type = Type::getType($this->_columnTypes[$columnName]); $placeholder = $type->convertToDatabaseValueSQL('?', $this->_platform); } + } else if(isset($this->quotedColumns[$columnName])) { + $column = $this->quotedColumns[$columnName]; } $set[] = $column . ' = ' . $placeholder; @@ -546,7 +557,13 @@ class BasicEntityPersister $targetClass = $this->_em->getClassMetadata($assoc['targetEntity']); $owningTable = $this->getOwningTable($field); - foreach ($assoc['sourceToTargetKeyColumns'] as $sourceColumn => $targetColumn) { + foreach ($assoc['joinColumns'] as $joinColumn) { + $sourceColumn = $joinColumn['name']; + $targetColumn = $joinColumn['referencedColumnName']; + + $quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->_class); + $this->quotedColumns[$sourceColumn] = $quotedColumn; + if ($newVal === null) { $result[$owningTable][$sourceColumn] = null; } else if ($targetClass->containsForeignIdentifier) { diff --git a/tests/Doctrine/Tests/Models/Quote/Address.php b/tests/Doctrine/Tests/Models/Quote/Address.php index 3597687d5..58e303f13 100644 --- a/tests/Doctrine/Tests/Models/Quote/Address.php +++ b/tests/Doctrine/Tests/Models/Quote/Address.php @@ -27,4 +27,28 @@ class Address */ public $user; + + public function setUser(User $user) { + if ($this->user !== $user) { + $this->user = $user; + $user->setAddress($this); + } + } + + + public function getId() + { + return $this->id; + } + + public function getZip() + { + return $this->zip; + } + + public function getUser() + { + return $this->user; + } + } \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/Quote/User.php b/tests/Doctrine/Tests/Models/Quote/User.php index 7d485aabe..72715d783 100644 --- a/tests/Doctrine/Tests/Models/Quote/User.php +++ b/tests/Doctrine/Tests/Models/Quote/User.php @@ -10,7 +10,6 @@ use Doctrine\Common\Collections\ArrayCollection; */ class User { - /** * @Id * @GeneratedValue @@ -30,7 +29,7 @@ class User /** * @JoinColumn(name="`address-id`", referencedColumnName="`address-id`") - * @OneToOne(targetEntity="Address", mappedBy="user", cascade={"persist"}) + * @OneToOne(targetEntity="Address", mappedBy="user", cascade={"persist"}, fetch="EAGER") */ public $address; @@ -59,4 +58,26 @@ class User $this->groups = new ArrayCollection; } + + public function getPhones() + { + return $this->phones; + } + + public function getAddress() + { + return $this->address; + } + + public function getGroups() + { + return $this->groups; + } + + public function setAddress(Address $address) { + if ($this->address !== $address) { + $this->address = $address; + $address->setUser($this); + } + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC142Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC142Test.php new file mode 100644 index 000000000..923d495f4 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC142Test.php @@ -0,0 +1,63 @@ +_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); + + 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\Phone'), + $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Address'), + )); + } catch(\Exception $e) { + //$this->fail($e->getMessage()); + } + + } + + public function testCreateRetreaveUpdateDelete() + { + + $user = new User; + $user->name = 'FabioBatSilva'; + $this->_em->persist($user); + + $address = new Address; + $address->zip = '12345'; + $this->_em->persist($address); + + $this->_em->flush(); + + $addressRef = $this->_em->getReference('Doctrine\Tests\Models\Quote\Address', $address->getId()); + + $user->setAddress($addressRef); + + $this->_em->flush(); + $this->_em->clear(); + + $this->assertNotNull($user->id); + $this->markTestIncomplete(); + + $user = $this->_em->find('Doctrine\Tests\Models\Quote\User', $user->id); + + } + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php index 9f6a17ab4..15cfd5903 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1843Test.php @@ -7,19 +7,16 @@ use Doctrine\Tests\Models\Quote\Group; require_once __DIR__ . '/../../../TestInit.php'; /** + * @group DDC-1845 * @group DDC-1843 */ class DDC1843Test extends \Doctrine\Tests\OrmFunctionalTestCase { - const CLASS_NAME = '\Doctrine\Tests\Models\Quote\Group'; - protected function setUp() { parent::setUp(); - //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); - try { $this->_schemaTool->createSchema(array( $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\User'), @@ -28,7 +25,6 @@ class DDC1843Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\Address'), )); } catch(\Exception $e) { - $this->fail($e->getMessage()); } } @@ -57,15 +53,15 @@ class DDC1843Test extends \Doctrine\Tests\OrmFunctionalTestCase $e4Id = $e4->id; // Retreave - $e1 = $this->_em->find(self::CLASS_NAME, $e1Id); - $e2 = $this->_em->find(self::CLASS_NAME, $e2Id); - $e3 = $this->_em->find(self::CLASS_NAME, $e3Id); - $e4 = $this->_em->find(self::CLASS_NAME, $e4Id); + $e1 = $this->_em->find('Doctrine\Tests\Models\Quote\Group', $e1Id); + $e2 = $this->_em->find('Doctrine\Tests\Models\Quote\Group', $e2Id); + $e3 = $this->_em->find('Doctrine\Tests\Models\Quote\Group', $e3Id); + $e4 = $this->_em->find('Doctrine\Tests\Models\Quote\Group', $e4Id); - $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->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e1); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e2); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e3); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e4); $this->assertEquals($e1Id, $e1->id); $this->assertEquals($e2Id, $e2->id); @@ -95,10 +91,10 @@ class DDC1843Test extends \Doctrine\Tests\OrmFunctionalTestCase $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->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e1); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e2); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e3); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e4); $this->assertEquals($e1Id, $e1->id); $this->assertEquals($e2Id, $e2->id); @@ -120,16 +116,16 @@ class DDC1843Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->clear(); - $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->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e1); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e2); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e3); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $e4); // Retreave - $e1 = $this->_em->find(self::CLASS_NAME, $e1Id); - $e2 = $this->_em->find(self::CLASS_NAME, $e2Id); - $e3 = $this->_em->find(self::CLASS_NAME, $e3Id); - $e4 = $this->_em->find(self::CLASS_NAME, $e4Id); + $e1 = $this->_em->find('Doctrine\Tests\Models\Quote\Group', $e1Id); + $e2 = $this->_em->find('Doctrine\Tests\Models\Quote\Group', $e2Id); + $e3 = $this->_em->find('Doctrine\Tests\Models\Quote\Group', $e3Id); + $e4 = $this->_em->find('Doctrine\Tests\Models\Quote\Group', $e4Id); $this->assertNull($e1); $this->assertNull($e2);