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

Commit 1ff1056

Browse files
authored
Add setGeneratorBasedOnId() in AbstractFixture
1 parent 9aaa164 commit 1ff1056

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

AbstractFixture.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ public function load(ObjectManager $manager)
135135
}
136136
}
137137

138+
/**
139+
* This allows you to change your ID generator if you are using IDs in your objects.
140+
* On reason to change this would be to use a GeneratorType constant instead of IdGenerator instance.
141+
* ID generation can be managed differently depending on your DBMS: sqlite, mysql, postgres, etc.,
142+
* all react differently...
143+
*
144+
* @param ClassMetadata $metadata
145+
* @param null $id
146+
*/
147+
protected function setGeneratorBasedOnId(ClassMetadata $metadata, $id = null)
148+
{
149+
if ($id) {
150+
$metadata->setIdGenerator(new AssignedGenerator());
151+
} else {
152+
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
153+
}
154+
}
155+
138156
/**
139157
* Creates the object and persist it in database.
140158
*
@@ -200,10 +218,8 @@ private function fixtureObject($data)
200218
}
201219
}
202220

203-
// If the ID is set, we tell Doctrine to force the insertion of it.
204-
if ($id) {
205-
$metadata->setIdGenerator(new AssignedGenerator());
206-
}
221+
// Set the id generator in case it is overriden.
222+
$this->setGeneratorBasedOnId($metadata, $id);
207223

208224
// And finally we persist the item
209225
$this->manager->persist($obj);

0 commit comments

Comments
 (0)