This repository was archived by the owner on Nov 17, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change 13
13
14
14
use Closure ;
15
15
use Doctrine \Common \DataFixtures \AbstractFixture as BaseAbstractFixture ;
16
+ use Doctrine \ORM \Id \AssignedGenerator ;
16
17
use Doctrine \Common \DataFixtures \OrderedFixtureInterface ;
17
18
use Doctrine \Common \Persistence \ObjectManager ;
18
19
use Doctrine \Instantiator \Instantiator ;
@@ -119,6 +120,18 @@ private function fixtureObject(array $data): void
119
120
{
120
121
$ obj = $ this ->createNewInstance ($ data );
121
122
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
+
122
135
// And finally we persist the item
123
136
$ this ->manager ->persist ($ obj );
124
137
You can’t perform that action at this time.
0 commit comments