1
0
mirror of synced 2025-01-18 22:41:43 +03:00

DDC-1250 - Fix bug with inverse one to one loading and ambigious column names in certain scenarios

This commit is contained in:
Benjamin Eberlei 2011-07-09 15:40:10 +02:00
parent 8b92dc5265
commit c261315ea7
4 changed files with 113 additions and 3 deletions

View File

@ -633,7 +633,11 @@ class BasicEntityPersister
// TRICKY: since the association is specular source and target are flipped
foreach ($owningAssoc['targetToSourceKeyColumns'] as $sourceKeyColumn => $targetKeyColumn) {
if (isset($sourceClass->fieldNames[$sourceKeyColumn])) {
$identifier[$targetKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
// unset the old value and set the new sql aliased value here. By definition
// unset($identifier[$targetKeyColumn] works here with how UnitOfWork::createEntity() calls this method.
$identifier[$this->_getSQLTableAlias($targetClass->name) . "." . $targetKeyColumn] =
$sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
unset($identifier[$targetKeyColumn]);
} else {
throw MappingException::joinColumnMustPointToMappedField(
$sourceClass->name, $sourceKeyColumn
@ -1214,7 +1218,7 @@ class BasicEntityPersister
} else if ($assoc !== null && strpos($field, " ") === false && strpos($field, "(") === false) {
// very careless developers could potentially open up this normally hidden api for userland attacks,
// therefore checking for spaces and function calls which are not allowed.
// found a join column condition, not really a "field"
$conditionSql .= $field;
} else {

View File

@ -34,7 +34,7 @@ class ECommerceCustomer
* only one customer at the time, while a customer can choose only one
* mentor. Not properly appropriate but it works.
*
* @OneToOne(targetEntity="ECommerceCustomer", cascade={"persist"})
* @OneToOne(targetEntity="ECommerceCustomer", cascade={"persist"}, fetch="EAGER")
* @JoinColumn(name="mentor_id", referencedColumnName="id")
*/
private $mentor;

View File

@ -49,6 +49,14 @@ class OneToOneSelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunction
$this->assertForeignKeyIs(null);
}
public function testFind()
{
$id = $this->_createFixture();
$customer = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceCustomer', $id);
$this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $customer->getMentor());
}
public function testEagerLoadsAssociation()
{
$this->_createFixture();
@ -127,6 +135,8 @@ class OneToOneSelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunction
$this->_em->flush();
$this->_em->clear();
return $customer->getId();
}
}

View File

@ -0,0 +1,96 @@
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Tests\Models\CMS\CmsEmployee;
require_once __DIR__ . '/../../../TestInit.php';
/**
* @group DDC-1250
*/
class DDC1250Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
parent::setUp();
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1250ClientHistory'),
));
} catch(\PDOException $e) {
}
}
public function testIssue()
{
$c1 = new DDC1250ClientHistory;
$c2 = new DDC1250ClientHistory;
$c1->declinedClientsHistory = $c2;
$c1->declinedBy = $c2;
$c2->declinedBy = $c1;
$c2->declinedClientsHistory= $c1;
$this->_em->persist($c1);
$this->_em->persist($c2);
$this->_em->flush();
$this->_em->clear();
$history = $this->_em->createQuery('SELECT h FROM ' . __NAMESPACE__ . '\\DDC1250ClientHistory h WHERE h.id = ?1')
->setParameter(1, $c2->id)->getSingleResult();
$this->assertInstanceOf(__NAMESPACE__ . '\\DDC1250ClientHistory', $history);
}
}
/**
* @Entity
*/
class DDC1250ClientHistory
{
/** @Id @GeneratedValue @Column(type="integer") */
public $id;
/** @OneToOne(targetEntity="DDC1250ClientHistory", inversedBy="declinedBy")
* @JoinColumn(name="declined_clients_history_id", referencedColumnName="id")
*/
public $declinedClientsHistory;
/**
* @OneToOne(targetEntity="DDC1250ClientHistory", mappedBy="declinedClientsHistory")
* @var
*/
public $declinedBy;
}
/**
*
Entities\ClientsHistory:
type: entity
table: clients_history
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
[...skiped...]
oneToOne:
declinedClientsHistory:
targetEntity: Entities\ClientsHistory
joinColumn:
name: declined_clients_history_id
referencedColumnName: id
inversedBy: declinedBy
declinedBy:
targetEntity: Entities\ClientsHistory
mappedBy: declinedClientsHistory
lifecycleCallbacks: { }
repositoryClass: Entities\ClientsHistoryRepository
*/