Answer the question
In order to leave comments, you need to log in
How to specify id manually before recording?
I am transferring data from another database.
$sql = 'SELECT * FROM `user`';
// ...
$user = new User();
$user->setEmail($u['user_email']);
$user->setUsername($u['login']);
// ...
$em->persist($user);
$em->flush();
@ORM\GeneratedValue(strategy="IDENTITY")
.
Answer the question
In order to leave comments, you need to log in
You need to add the code before persist:
$metadata = $em->getClassMetadata($user);
$metadata->setIdGeneratorType($metadata::GENERATOR_TYPE_NONE);
$metadata->setIdGenerator(new AssignedGenerator());
After saving, if necessary, restore the generator
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
$metadata->setIdGenerator(new IdentityGenerator(null));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question