DDC-1585 - Throw exception if setting target entity of the wrong type to an assocation.
This commit is contained in:
parent
3c391f8f37
commit
36ce26691d
@ -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']) {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user