Persist Object with Two Foreign Identities in Doctrine

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:

    // create a new package and catalogue for the import
    $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);

    // load the file, and create a new code/translation combination for every message
    $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(); //FIXME no flush in between, if possible
        $this->em->persist($translate);
    }

    // save all the changes to the database
    $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() .

+3
2

, Doctrine Docs flush :

/ , . persist. . , .

+4

- ?

YAML:

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
        cascade: ["persist"]
        inversedBy: tr_codes
        joinColumn:
            name: idCodes
            referencedColumnName: id
    catalogue:
        targetEntity: Catalogue
        cascade: ["persist"]
        inversedBy: tr_catalogues
        joinColumn:
            name: idCatalogues
            referencedColumnName: id

Translation

public function __construct()
{
    $this->code = new \Doctrine\Common\Collections\ArrayCollection();
    $this->catalogue = new \Doctrine\Common\Collections\ArrayCollection();
}

public function addCatalogue($catalogue)
{
    $this->catalogue[] = $catalogue;

    return $this;
}

public function addCode($code)
{
    $this->code[] = $code;

    return $this;
}

:

$package = new Package();
$package->setName($this->getName());
$catalogue = new Catalogue();
$catalogue->setLocale($this->getLocale())->setPackage($package);

$fileCatalogue = $loader->load($this->getFile(), $this->getLocale());
foreach ($fileCatalogue->all()['messages'] as $key => $message) {
    $code = new Code();
    $code->setPackage($package)
         ->setCode($key)
         ->setBackend(true)
         ->setFrontend(true);
    $translate = new Translation();
    $translate->addCode($code)
              ->setValue($message)
              ->addCatalogue($catalogue);
    $this->em->persist($translate);
}

$this->em->flush();

: : [ "persist" ] - .

0

All Articles