I created an object using the yml syntax in my symfony collection, in the Resources / config / doctrine folder:
Sulu\Bundle\TranslateBundle\Entity\Translation:
type: entity
table: tr_translations
id:
code:
type: string
column: idCodes
associationKey: id
catalogue:
type: string
column: idCatalogues
associationKey: id
fields:
value:
type: text
manyToOne:
code:
targetEntity: Code
inversedBy: tr_codes
joinColumn:
name: idCodes
referencedColumnName: id
catalogue:
targetEntity: Catalogue
inversedBy: tr_catalogues
joinColumn:
name: idCatalogues
referencedColumnName: id
This part is working correctly. But when I create some objects, as in the following code, I get an error that I have to use the flush method to get identifiers for foreign keys.
This is the piece of code that I am currently using:
$package = new Package();
$package->setName($this->getName());
$catalogue = new Catalogue();
$catalogue->setLocale($this->getLocale());
$catalogue->setPackage($package);
$this->em->persist($package);
$this->em->persist($catalogue);
$fileCatalogue = $loader->load($this->getFile(), $this->getLocale());
foreach ($fileCatalogue->all()['messages'] as $key => $message) {
$code = new Code();
$code->setPackage($package);
$code->setCode($key);
$code->setBackend(true);
$code->setFrontend(true);
$translate = new Translation();
$translate->setCode($code);
$translate->setValue($message);
$translate->setCatalogue($catalogue);
$this->em->persist($code);
$this->em->flush();
$this->em->persist($translate);
}
$this->em->flush();
If I don't call a flash in the foreach loop, I get the following error, which I fully understand, but is there no more elegant solution to this problem?
Doctrine\ORM\ORMException: Sulu\Bundle\TranslateBundle\Entity\Translation Sulu\Bundle\TranslateBundle\Entity\Code, . EntityManager # persist() , , '\Bundle\TranslateBundle\Entity\Translation'. (, MySQL Auto-Increment PostgreSQL SERIAL), , EntityManager # flush() .