mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
DDC-993: Cookbook: Overriding the ID Generator during a database migration #1239
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Jan 19, 2011).
Originally assigned to: @beberlei on GitHub.
Jira issue originally created by user felicitus:
If you need to override the ID Generator, e.g. during a migration, you can do that in your migration script as follows:
{panel:title=Overriding the ID generator}
$em->getClassMetadata('foo\bar\Entity')->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
$em->getClassMetadata('foo\bar\Entity')->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE'));
{panel}
Make sure that both calls equal to the same generator type. You can now modify the @Id fields in your entities. Additionally, make sure that you set the IdGenerator after you created the database using e.g. SchemaTool->create().
@doctrinebot commented on GitHub (Oct 27, 2012):
Comment created by kosen:
Hi, this doesn't seem to work for me. I have written a small database export / import utility. As long as I use the automatic ID generation, everything works flawlessly, but I'm trying to preserve the existing IDs. I do exactly what you've suggested in your post. It works for @OneToOne relations, but I get the following error messages when persisting entities that are parts of @ManyToOne relations:
Notice: Undefined index: [....] in [...]Doctrine/ORM/UnitOfWork.php on line 2655
I'm using version 2.2.2
Am I doing something wrong?
@doctrinebot commented on GitHub (Oct 28, 2012):
Comment created by kosen:
Never mind. I've upgraded to Doctrine 2.3.0 and it works as expected.