diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index cd433a7fa..d075d418c 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -378,16 +378,22 @@ use Doctrine\Common\Util\ClassUtils; { $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); - if (is_object($id) && $this->metadataFactory->hasMetadataFor(ClassUtils::getClass($id))) { - $id = $this->unitOfWork->getSingleIdentifierValue($id); - - if ($id === null) { - throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); + if ( ! is_array($id)) { + if ($class->isIdentifierComposite) { + throw ORMInvalidArgumentException::invalidCompositeIdentifier(); } + + $id = array($class->identifier[0] => $id); } - if ( ! is_array($id)) { - $id = array($class->identifier[0] => $id); + foreach ($id as $i => $value) { + if (is_object($value) && $this->metadataFactory->hasMetadataFor(ClassUtils::getClass($value))) { + $id[$i] = $this->unitOfWork->getSingleIdentifierValue($value); + + if ($id[$i] === null) { + throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); + } + } } $sortedId = array(); diff --git a/tests/Doctrine/Tests/Models/Taxi/Car.php b/tests/Doctrine/Tests/Models/Taxi/Car.php index 2db5ecd36..03cc74c96 100644 --- a/tests/Doctrine/Tests/Models/Taxi/Car.php +++ b/tests/Doctrine/Tests/Models/Taxi/Car.php @@ -29,6 +29,11 @@ class Car * @OneToMany(targetEntity="PaidRide", mappedBy="car") */ private $carRides; + + public function getBrand() + { + return $this->brand; + } public function setBrand($brand) { diff --git a/tests/Doctrine/Tests/Models/Taxi/Driver.php b/tests/Doctrine/Tests/Models/Taxi/Driver.php index ccb493f1d..ff0cb41a4 100644 --- a/tests/Doctrine/Tests/Models/Taxi/Driver.php +++ b/tests/Doctrine/Tests/Models/Taxi/Driver.php @@ -29,6 +29,11 @@ class Driver * @OneToMany(targetEntity="PaidRide", mappedBy="driver") */ private $driverRides; + + public function getId() + { + return $this->id; + } public function setName($name) { diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3068Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3068Test.php new file mode 100644 index 000000000..d43e35563 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3068Test.php @@ -0,0 +1,58 @@ + + */ +class DDC3068Test extends \Doctrine\Tests\OrmFunctionalTestCase +{ + private $foo; + private $merc; + + protected function setUp() + { + $this->useModelSet('taxi'); + parent::setUp(); + + $this->foo = new Driver(); + $this->foo->setName('Foo Bar'); + $this->_em->persist($this->foo); + + $this->merc = new Car(); + $this->merc->setBrand('Mercedes'); + $this->merc->setModel('C-Class'); + $this->_em->persist($this->merc); + + $this->_em->flush(); + + $ride = new Ride($this->foo, $this->merc); + $this->_em->persist($ride); + + $this->_em->flush(); + } + + public function testFindUsingAnArrayOfObjectAsPrimaryKey() + { + $ride1 = $this->_em->find('Doctrine\Tests\Models\Taxi\Ride', array( + 'driver' => $this->foo->getId(), + 'car' => $this->merc->getBrand()) + ); + + $this->assertInstanceOf('Doctrine\Tests\Models\Taxi\Ride', $ride1); + + $ride2 = $this->_em->find('Doctrine\Tests\Models\Taxi\Ride', array( + 'driver' => $this->foo, + 'car' => $this->merc + )); + + $this->assertInstanceOf('Doctrine\Tests\Models\Taxi\Ride', $ride2); + $this->assertSame($ride1, $ride2); + } +}