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

Commit

Permalink
Add setGeneratorBasedOnId() in AbstractFixture
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval authored Aug 23, 2016
1 parent 9aaa164 commit 1ff1056
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ public function load(ObjectManager $manager)
}
}

/**
* This allows you to change your ID generator if you are using IDs in your objects.
* On reason to change this would be to use a GeneratorType constant instead of IdGenerator instance.
* ID generation can be managed differently depending on your DBMS: sqlite, mysql, postgres, etc.,
* all react differently...
*
* @param ClassMetadata $metadata
* @param null $id
*/
protected function setGeneratorBasedOnId(ClassMetadata $metadata, $id = null)
{
if ($id) {
$metadata->setIdGenerator(new AssignedGenerator());
} else {
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
}
}

/**
* Creates the object and persist it in database.
*
Expand Down Expand Up @@ -200,10 +218,8 @@ private function fixtureObject($data)
}
}

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

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

0 comments on commit 1ff1056

Please sign in to comment.