1
0
mirror of synced 2025-02-20 14:13:15 +03:00

DDC-1585 - Throw exception if setting target entity of the wrong type to an assocation.

This commit is contained in:
Benjamin Eberlei 2012-01-15 14:58:56 +01:00
parent 3c391f8f37
commit 36ce26691d
2 changed files with 26 additions and 0 deletions

View File

@ -707,6 +707,15 @@ class UnitOfWork implements PropertyChangedListener
$state = $this->getEntityState($entry, self::STATE_NEW);
$oid = spl_object_hash($entry);
if (!($entry instanceof $assoc['targetEntity'])) {
throw new ORMException(sprintf("Found entity of type %s on association %s#%s, but expecting %s",
get_class($entry),
$assoc['sourceEntity'],
$assoc['fieldName'],
$targetClass->name
));
}
switch ($state) {
case self::STATE_NEW:
if ( ! $assoc['isCascadePersist']) {

View File

@ -1197,4 +1197,21 @@ class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase
$user2 = $this->_em->find(get_class($user2), $user2->id);
$this->assertEquals('developer', $user2->status);
}
/**
* @group DDC-1585
*/
public function testWrongAssocationInstance()
{
$user = new CmsUser;
$user->name = 'Dominik';
$user->username = 'domnikl';
$user->status = 'developer';
$user->address = $user;
$this->_em->persist($user);
$this->setExpectedException("Doctrine\ORM\ORMException", "Found entity of type Doctrine\Tests\Models\CMS\CmsUser on association Doctrine\Tests\Models\CMS\CmsUser#address, but expecting Doctrine\Tests\Models\CMS\CmsAddress");
$this->_em->flush();
}
}