Setting manually generators #5421

Closed
opened 2026-01-22 15:07:20 +01:00 by admin · 5 comments
Owner

Originally created by @tabbi89 on GitHub (Feb 20, 2017).

Originally assigned to: @Ocramius on GitHub.

I have encounter problem where I have entity which has property GeneratedValue id with strategy AUTO

What I tried to achieve was to add two Category entities with one set id manually.

/**
 * @var $managerRegistry \Doctrine\Bundle\DoctrineBundle\Registry
 */
$manager = $managerRegistry->getManagerForClass(\AppBundle\Entity\Category::class);

$metadata = $manager->getClassMetadata(\AppBundle\Entity\Category::class);
$category = new \AppBundle\Entity\Category();
$category->setText('hello');

$manager->persist($category);
// $manager->flush();

$category = new \AppBundle\Entity\Category();
$category->setText('hello 2');
$category->setId(12);

$metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
$metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);

$manager->persist($category);
$manager->flush();

This is how it works but I when I uncomment first flush I get error during adding second entity to database:

An exception occurred while executing 'INSERT INTO category (text) VALUES (?)' with params [12, "hello 2"]:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens (Doctrine\DBAL\Exception\DriverException

As we see param with id is set but value is not mapped. My question is: Is this a desire behaviour ?

Originally created by @tabbi89 on GitHub (Feb 20, 2017). Originally assigned to: @Ocramius on GitHub. I have encounter problem where I have entity which has property `GeneratedValue` id with strategy `AUTO` What I tried to achieve was to add two Category entities with one set id manually. ``` /** * @var $managerRegistry \Doctrine\Bundle\DoctrineBundle\Registry */ $manager = $managerRegistry->getManagerForClass(\AppBundle\Entity\Category::class); $metadata = $manager->getClassMetadata(\AppBundle\Entity\Category::class); $category = new \AppBundle\Entity\Category(); $category->setText('hello'); $manager->persist($category); // $manager->flush(); $category = new \AppBundle\Entity\Category(); $category->setText('hello 2'); $category->setId(12); $metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator()); $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE); $manager->persist($category); $manager->flush(); ``` This is how it works but I when I uncomment first flush I get error during adding second entity to database: ``` An exception occurred while executing 'INSERT INTO category (text) VALUES (?)' with params [12, "hello 2"]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens (Doctrine\DBAL\Exception\DriverException ``` As we see param with id is set but value is not mapped. My question is: Is this a desire behaviour ?
admin added the BugInvalid labels 2026-01-22 15:07:20 +01:00
admin closed this issue 2026-01-22 15:07:20 +01:00
Author
Owner

@Ocramius commented on GitHub (Feb 20, 2017):

@tabbi89 you can't really change mapping information at runtime: can you try this by changing metadata before executing these operations?

@Ocramius commented on GitHub (Feb 20, 2017): @tabbi89 you can't really change mapping information at runtime: can you try this by changing metadata before executing these operations?
Author
Owner

@tabbi89 commented on GitHub (Feb 20, 2017):

@Ocramius I am using this in behat scenarios - so I have background step which adds admin user and second step which adds specified user with concrete id :) I would have to change mapping for all but that includes generating ids in for this entity in all steps.

Any other option that you suggest ?

@tabbi89 commented on GitHub (Feb 20, 2017): @Ocramius I am using this in behat scenarios - so I have background step which adds admin user and second step which adds specified user with concrete id :) I would have to change mapping for all but that includes generating ids in for this entity in all steps. Any other option that you suggest ?
Author
Owner

@Ocramius commented on GitHub (Feb 20, 2017):

@Ocramius I am using this in behat scenarios - so I have background step which adds admin user and second step which adds specified user with concrete id :) I would have to change mapping for all but that includes generating ids in for this entity in all steps.

That's what a fixture would do for you. Changing mapping at runtime is not something supported. If you can't reproduce this failure without the runtime mapping change, I'll close this as invalid

@Ocramius commented on GitHub (Feb 20, 2017): > @Ocramius I am using this in behat scenarios - so I have background step which adds admin user and second step which adds specified user with concrete id :) I would have to change mapping for all but that includes generating ids in for this entity in all steps. That's what a fixture would do for you. Changing mapping at runtime is not something supported. If you can't reproduce this failure without the runtime mapping change, I'll close this as `invalid`
Author
Owner

@tabbi89 commented on GitHub (Feb 20, 2017):

Thanks, you can close it as invalid

@tabbi89 commented on GitHub (Feb 20, 2017): Thanks, you can close it as invalid
Author
Owner

@Ocramius commented on GitHub (Feb 20, 2017):

@tabbi89 thanks for clarifying! 👍

@Ocramius commented on GitHub (Feb 20, 2017): @tabbi89 thanks for clarifying! 👍
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5421