Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 354ccdc

Browse files
author
Bill Schaller
committed
Merge pull request doctrine#1361 from Ocramius/hotfix/array-property-initialization
Hotfix: Reverting BC Break - PersistentCollection should accept `null` and `array` as constructor parameter
2 parents ea824f3 + 2a81adc commit 354ccdc

8 files changed

Lines changed: 72 additions & 14 deletions

File tree

lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ private function initRelatedCollection($entity, $class, $fieldName, $parentDqlAl
189189
$relation = $class->associationMappings[$fieldName];
190190
$value = $class->reflFields[$fieldName]->getValue($entity);
191191

192-
if ($value === null) {
193-
$value = new ArrayCollection;
192+
if ($value === null || is_array($value)) {
193+
$value = new ArrayCollection((array) $value);
194194
}
195195

196196
if ( ! $value instanceof PersistentCollection) {

lib/Doctrine/ORM/PersistentCollection.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
101101
/**
102102
* Creates a new persistent collection.
103103
*
104-
* @param EntityManagerInterface $em The EntityManager the collection will be associated with.
105-
* @param ClassMetadata $class The class descriptor of the entity type of this collection.
106-
* @param Collection $coll The collection elements.
104+
* @param EntityManagerInterface $em The EntityManager the collection will be associated with.
105+
* @param ClassMetadata $class The class descriptor of the entity type of this collection.
106+
* @param Collection $collection The collection elements.
107107
*/
108-
public function __construct(EntityManagerInterface $em, $class, $coll)
108+
public function __construct(EntityManagerInterface $em, $class, Collection $collection)
109109
{
110-
$this->collection = $coll;
110+
$this->collection = $collection;
111111
$this->em = $em;
112112
$this->typeClass = $class;
113113
$this->initialized = true;

lib/Doctrine/ORM/Query/ResultSetMapping.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ public function addFieldResult($alias, $columnName, $fieldName, $declaringClass
347347
* @param string $class The class name of the joined entity.
348348
* @param string $alias The unique alias to use for the joined entity.
349349
* @param string $parentAlias The alias of the entity result that is the parent of this joined result.
350-
* @param object $relation The association field that connects the parent entity result
350+
* @param string $relation The association field that connects the parent entity result
351351
* with the joined entity result.
352352
*
353353
* @return ResultSetMapping This ResultSetMapping instance.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\Models\Hydration;
4+
5+
/** @Entity */
6+
class EntityWithArrayDefaultArrayValueM2M
7+
{
8+
const CLASSNAME = __CLASS__;
9+
10+
/** @Id @Column(type="integer") @GeneratedValue(strategy="AUTO") */
11+
public $id;
12+
13+
/** @ManyToMany(targetEntity=SimpleEntity::class) */
14+
public $collection = [];
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\Models\Hydration;
4+
5+
/** @Entity */
6+
class SimpleEntity
7+
{
8+
const CLASSNAME = __CLASS__;
9+
10+
/** @Id @Column(type="integer") @GeneratedValue(strategy="AUTO") */
11+
public $id;
12+
}

tests/Doctrine/Tests/ORM/Cache/Persister/Entity/AbstractEntityPersisterTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Doctrine\Tests\Models\Cache\Country;
1313
use Doctrine\Common\Collections\Criteria;
14+
use Doctrine\Common\Collections\ArrayCollection;
1415
use Doctrine\ORM\Query\ResultSetMappingBuilder;
1516
use Doctrine\ORM\PersistentCollection;
1617

@@ -390,7 +391,7 @@ public function testInvokeLoadManyToManyCollection()
390391
{
391392
$mapping = $this->em->getClassMetadata('Doctrine\Tests\Models\Cache\Country');
392393
$assoc = array('type' => 1);
393-
$coll = new PersistentCollection($this->em, $mapping, null);
394+
$coll = new PersistentCollection($this->em, $mapping, new ArrayCollection());
394395
$persister = $this->createPersisterDefault();
395396
$entity = new Country("Foo");
396397

@@ -406,7 +407,7 @@ public function testInvokeLoadOneToManyCollection()
406407
{
407408
$mapping = $this->em->getClassMetadata('Doctrine\Tests\Models\Cache\Country');
408409
$assoc = array('type' => 1);
409-
$coll = new PersistentCollection($this->em, $mapping, null);
410+
$coll = new PersistentCollection($this->em, $mapping, new ArrayCollection());
410411
$persister = $this->createPersisterDefault();
411412
$entity = new Country("Foo");
412413

tests/Doctrine/Tests/ORM/Hydration/ObjectHydratorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Doctrine\ORM\Mapping\AssociationMapping;
99
use Doctrine\ORM\Mapping\ClassMetadata;
1010
use Doctrine\ORM\Query;
11+
use Doctrine\Tests\Models\Hydration\EntityWithArrayDefaultArrayValueM2M;
12+
use Doctrine\Tests\Models\Hydration\SimpleEntity;
1113

1214
use Doctrine\Tests\Models\CMS\CmsUser;
1315

@@ -1956,4 +1958,29 @@ public function testInvalidDiscriminatorValueException()
19561958
$hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
19571959
$hydrator->hydrateAll($stmt, $rsm);
19581960
}
1961+
1962+
public function testFetchJoinCollectionValuedAssociationWithDefaultArrayValue()
1963+
{
1964+
$rsm = new ResultSetMapping;
1965+
1966+
$rsm->addEntityResult(EntityWithArrayDefaultArrayValueM2M::CLASSNAME, 'e1', null);
1967+
$rsm->addJoinedEntityResult(SimpleEntity::CLASSNAME, 'e2', 'e1', 'collection');
1968+
$rsm->addFieldResult('e1', 'a1__id', 'id');
1969+
$rsm->addFieldResult('e2', 'e2__id', 'id');
1970+
1971+
$result = (new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em))
1972+
->hydrateAll(
1973+
new HydratorMockStatement([[
1974+
'a1__id' => '1',
1975+
'e2__id' => '1',
1976+
]]),
1977+
$rsm
1978+
);
1979+
1980+
$this->assertCount(1, $result);
1981+
$this->assertInstanceOf(EntityWithArrayDefaultArrayValueM2M::CLASSNAME, $result[0]);
1982+
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $result[0]->collection);
1983+
$this->assertCount(1, $result[0]->collection);
1984+
$this->assertInstanceOf(SimpleEntity::CLASSNAME, $result[0]->collection[0]);
1985+
}
19591986
}

tests/Doctrine/Tests/ORM/PersistentCollectionTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Doctrine\Tests\ORM;
44

55
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
67
use Doctrine\ORM\PersistentCollection;
78
use Doctrine\Tests\Mocks\ConnectionMock;
9+
use Doctrine\Tests\Mocks\DriverMock;
810
use Doctrine\Tests\Mocks\EntityManagerMock;
911
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
1012
use Doctrine\Tests\OrmTestCase;
@@ -21,15 +23,16 @@ class PersistentCollectionTest extends OrmTestCase
2123
*/
2224
protected $collection;
2325

24-
private $_connectionMock;
26+
/**
27+
* @var \Doctrine\ORM\EntityManagerInterface
28+
*/
2529
private $_emMock;
2630

2731
protected function setUp()
2832
{
2933
parent::setUp();
30-
// SUT
31-
$this->_connectionMock = new ConnectionMock(array(), new \Doctrine\Tests\Mocks\DriverMock());
32-
$this->_emMock = EntityManagerMock::create($this->_connectionMock);
34+
35+
$this->_emMock = EntityManagerMock::create(new ConnectionMock([], new DriverMock()));
3336
}
3437

3538
/**

0 commit comments

Comments
 (0)