Note that there are some explanatory texts on larger screens.

plurals
  1. POPersist object with two foreign identities in doctrine
    primarykey
    data
    text
    <p>I have created an entity using the yml-syntax in my symfony bundle, in the Resources/config/doctrine folder:</p> <pre><code>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 </code></pre> <p>This part is working correctly. But when I create some objects like in the following code, I get an error message that I have to use the flush method, in order to get IDs for the foreign keys.</p> <p>This is the code snippet I am currently using:</p> <pre><code> // create a new package and catalogue for the import $package = new Package(); $package-&gt;setName($this-&gt;getName()); $catalogue = new Catalogue(); $catalogue-&gt;setLocale($this-&gt;getLocale()); $catalogue-&gt;setPackage($package); $this-&gt;em-&gt;persist($package); $this-&gt;em-&gt;persist($catalogue); // load the file, and create a new code/translation combination for every message $fileCatalogue = $loader-&gt;load($this-&gt;getFile(), $this-&gt;getLocale()); foreach ($fileCatalogue-&gt;all()['messages'] as $key =&gt; $message) { $code = new Code(); $code-&gt;setPackage($package); $code-&gt;setCode($key); $code-&gt;setBackend(true); $code-&gt;setFrontend(true); $translate = new Translation(); $translate-&gt;setCode($code); $translate-&gt;setValue($message); $translate-&gt;setCatalogue($catalogue); $this-&gt;em-&gt;persist($code); $this-&gt;em-&gt;flush(); //FIXME no flush in between, if possible $this-&gt;em-&gt;persist($translate); } // save all the changes to the database $this-&gt;em-&gt;flush(); </code></pre> <p>If I don't call the flush in the foreach loop I get the following error, which I totally understand, but isn't there a more elegant solution for this problem?</p> <blockquote> <p>Doctrine\ORM\ORMException : Entity of type Sulu\Bundle\TranslateBundle\Entity\Translation has identity through a foreign entity Sulu\Bundle\TranslateBundle\Entity\Code, however this entity has no identity itself. You have to call EntityManager#persist() on the related entity and make sure that an identifier was generated before trying to persist 'Sulu\Bundle\TranslateBundle\Entity\Translation'. In case of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call EntityManager#flush() between both persist operations.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload