I have created an entity using the yml-syntax in my symfony bundle, 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 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.

This is the code snippet 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 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?

> 
> 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.
> 
> 
>