DDC-3068 DDC-3069 EntityManager::find accept array of object as id.
This commit is contained in:
parent
54d9f05e39
commit
10a0daf620
4 changed files with 81 additions and 7 deletions
|
@ -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();
|
||||
|
|
|
@ -29,6 +29,11 @@ class Car
|
|||
* @OneToMany(targetEntity="PaidRide", mappedBy="car")
|
||||
*/
|
||||
private $carRides;
|
||||
|
||||
public function getBrand()
|
||||
{
|
||||
return $this->brand;
|
||||
}
|
||||
|
||||
public function setBrand($brand)
|
||||
{
|
||||
|
|
|
@ -29,6 +29,11 @@ class Driver
|
|||
* @OneToMany(targetEntity="PaidRide", mappedBy="driver")
|
||||
*/
|
||||
private $driverRides;
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
|
|
58
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3068Test.php
Normal file
58
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3068Test.php
Normal file
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace Doctrine\Tests\ORM\Functional\Ticket;
|
||||
|
||||
use Doctrine\Tests\Models\Taxi\Car;
|
||||
use Doctrine\Tests\Models\Taxi\Driver;
|
||||
use Doctrine\Tests\Models\Taxi\Ride;
|
||||
|
||||
/**
|
||||
* @group DDC-3068
|
||||
*
|
||||
* @author Giorgio Premi <giosh94mhz@gmail.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue