I
I
Ivan Antonov2016-02-16 12:36:09
symfony
Ivan Antonov, 2016-02-16 12:36:09

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();

How to manually set the id before recording?
The id field is set to @ORM\GeneratedValue(strategy="IDENTITY").

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dizzy7, 2016-02-16
@antonowano

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 question

Ask a Question

731 491 924 answers to any question