Skip to content

Commit

Permalink
DDC-822 - Fix making queries with detached entities
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Sep 30, 2010
1 parent de236e0 commit 638c3df
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/Doctrine/ORM/Query.php
Expand Up @@ -242,10 +242,14 @@ protected function _doExecute()
}

if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(get_class($value))) {
//TODO: Check that $value is MANAGED?
$values = $this->_em->getUnitOfWork()->getEntityIdentifier($value);
if ($this->_em->getUnitOfWork()->getEntityState($value) == UnitOfWork::STATE_MANAGED) {
$idValues = $this->_em->getUnitOfWork()->getEntityIdentifier($value);
} else {
$class = $this->_em->getClassMetadata(get_class($value));
$idValuess = $class->getIdentifierValues($value);
}
$sqlPositions = $paramMappings[$key];
$sqlParams += array_combine((array)$sqlPositions, $values);
$sqlParams += array_combine((array)$sqlPositions, $idValues);
} else {
foreach ($paramMappings[$key] as $position) {
$sqlParams[$position] = $value;
Expand Down
29 changes: 27 additions & 2 deletions tests/Doctrine/Tests/ORM/Functional/DetachedEntityTest.php
Expand Up @@ -105,8 +105,8 @@ public function testDetachedEntityThrowsExceptionOnFlush()
} catch (\Exception $expected) {}
}

public function testUninitializedLazyAssociationsAreIgnoredOnMerge() {
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
public function testUninitializedLazyAssociationsAreIgnoredOnMerge()
{
$user = new CmsUser;
$user->name = 'Guilherme';
$user->username = 'gblanco';
Expand Down Expand Up @@ -136,5 +136,30 @@ public function testUninitializedLazyAssociationsAreIgnoredOnMerge() {
$this->assertFalse($managedAddress2->user === $detachedAddress2->user);
$this->assertFalse($managedAddress2->user->__isInitialized__);
}

/**
* @group DDC-822
*/
public function testUseDetachedEntityAsQueryParameter()
{
$user = new CmsUser;
$user->name = 'Guilherme';
$user->username = 'gblanco';
$user->status = 'developer';

$this->_em->persist($user);

$this->_em->flush();
$this->_em->detach($user);

$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
$query = $this->_em->createQuery($dql);
$query->setParameter(1, $user);

$newUser = $query->getSingleResult();

$this->assertType('Doctrine\Tests\Models\CMS\CmsUser', $newUser);
$this->assertEquals('gblanco', $newUser->username);
}
}

0 comments on commit 638c3df

Please sign in to comment.