fix DDC-142 persist OneToOne
This commit is contained in:
parent
51f29cddb9
commit
cd806b83db
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
63
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC142Test.php
Normal file
63
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC142Test.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\ORM\Functional\Ticket;
|
||||
|
||||
use Doctrine\Tests\Models\Quote\User;
|
||||
use Doctrine\Tests\Models\Quote\Address;
|
||||
|
||||
require_once __DIR__ . '/../../../TestInit.php';
|
||||
|
||||
/**
|
||||
* @group DDC-1845
|
||||
* @group DDC-142
|
||||
*/
|
||||
class DDC142Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
{
|
||||
|
||||
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'),
|
||||
$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);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user