Can I override auto zoom ID with Alice Fixtures?

I use Symfony2 with Doctrine, and I use the Alice Fixture package to build my test fixtures.

In one case, I need to create a device where the identifier is 148 for a specific test. All of our identifiers are set to auto_increment, so currently I'm creating 147 dummy entries to create the one I need.

I was hoping to use this definition to make id set to 148:

invoiceClassNoClass (extends invoiceClass): id: 148 sortOrder: 1 label: 'No Class' 

Unfortunately this will not work. From a Google search, I read a short comment saying that I first need to add the setId method to my object. I tried this, but it didn’t matter. Actually, I don’t want to add the setId method if I don’t need it, because it violates our integrity rules, where we never allow setting the identifier.

Maybe there is a reflection class that could be used? Perhaps this is embedded in Alice, and I do not know about it?

+7
symfony doctrine2 nelmio-alice
source share
3 answers

If you want the doctrine to store a specific value for the auto increment identifier, you must deactivate the auto increment in the metadata of the doctrine. Cm.

Explicitly set Id using Doctrine when using the "AUTO" Strategy

 $this->em->persist($entity); $metadata = $this->em->getClassMetaData(get_class($entity)); $metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator()); 
+6
source share

UPDATE : After further research on this issue, I realized that hard codes in fixtures have their own problems and, as a rule, are not a good idea. Instead, it is better to get the bindings by their link in the yml file when you need to check a specific record.


Since the question is about the Alice luminaire package, I found the following for me:

 protected $idGeneratorTypes = []; protected function allowFixedIdsFor(array $entityClasses) { $em = $this->getContainer()->get('doctrine')->getManager(); foreach ($entityClasses as $entityClass) { $metadata = $em->getClassMetadata($entityClass); $this->idGeneratorTypes[$entityClass] = $metadata->generatorType; $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE); } } protected function recoverIdGenerators() { $em = $this->getContainer()->get('doctrine')->getManager(); foreach ($this->idGeneratorTypes as $entityClass => $idGeneratorType) { $metadata = $em->getClassMetadata($entityClass); $metadata->setIdGeneratorType($idGeneratorType); } } 

Then in the setUp () test I have

  $this->allowFixedIdsFor([ MyEntity::class, ]); $this->loadFixtureFiles([ './tests/DataFixtures/ORM/my_entities.yml', ]); $this->recoverIdGenerators(); 

It looks like you don't need the setId () method in your entity.

+5
source share

I also ran into this problem, since I wanted some identifiers to be the same for every run, since I want to send them in debugging applications using the UUID strategy.

What I've done:

1) Wrote a command that wraps hautelook:fixtures:load :

 protected function execute(InputInterface $input, OutputInterface $output) { $container = $this->getContainer(); $entityManager = $container->get('doctrine.orm.default_entity_manager'); $metadata = $entityManager->getClassMetaData(App::class); $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE); $metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator()); $application = new Application($kernel); $application->setAutoExit(false); $this->loadFixtures($application, $output); } 

Make a $metadata material for all classes for which you want to have custom identifiers ( App::class ).

 protected function loadFixtures(Application $application, OutputInterface $output) { $loadFixturesInput = new ArrayInput(array( 'command' => 'hautelook:fixtures:load', '--no-interaction' => 'true', '--verbose' => '3' )); $application->run($loadFixturesInput, $output); } 

2) I created a template for creating the id similar doctrine.

 id (template): id (unique): '<uuid()>' 

3) In my custom object, I can now do the following:

 app_custom (extends id): id: 'whatever-i-want' title: 'My custom entity' 

4) And if I do not want to use a user identifier, I just do not overwrite the id field:

 app_another (extends id): title: 'Just another app with an id I do not care about' 

Evidence:

enter image description here

+5
source share

All Articles