diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 0ebdedcb9..6142ca958 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -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']) { diff --git a/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php b/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php index 9b68098c0..3955387e0 100644 --- a/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php @@ -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(); + } }