Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 517a321

Browse files
committed
Re-establish again the id generator override
1 parent 61143fb commit 517a321

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

AbstractFixture.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Closure;
1515
use Doctrine\Common\DataFixtures\AbstractFixture as BaseAbstractFixture;
16+
use Doctrine\ORM\Id\AssignedGenerator;
1617
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
1718
use Doctrine\Common\Persistence\ObjectManager;
1819
use Doctrine\Instantiator\Instantiator;
@@ -119,6 +120,18 @@ private function fixtureObject(array $data): void
119120
{
120121
$obj = $this->createNewInstance($data);
121122

123+
// Sometimes keys are specified in fixtures.
124+
// We must make sure Doctrine will force them to be saved.
125+
// Support for non-composite primary keys only.
126+
// /!\ Be careful, this will override the generator type for ALL objects of the same entity class!
127+
// This means that it _may_ break objects for which ids are not provided in the fixtures.
128+
$metadata = $this->manager->getClassMetadata($this->getEntityClass());
129+
$primaryKey = $metadata->getIdentifierFieldNames();
130+
if (1 === \count($primaryKey) && isset($data[$primaryKey[0]])) {
131+
$metadata->setIdGeneratorType($metadata::GENERATOR_TYPE_NONE);
132+
$metadata->setIdGenerator(new AssignedGenerator());
133+
}
134+
122135
// And finally we persist the item
123136
$this->manager->persist($obj);
124137

0 commit comments

Comments
 (0)