php-在理论中具有两个外来身份的持久对象

我已经在我的symfony包的Res​​ources / config / doctrine文件夹中使用yml-Syntax创建了一个实体:

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

这部分工作正常.但是,当我像下面的代码中那样创建一些对象时,我收到一条错误消息,我必须使用flush方法才能获取外键的ID.

这是我当前使用的代码片段:

    // 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();

如果我没有在foreach循环中调用flush,则会收到以下错误,我完全理解,但是对于这个问题没有更好的解决方案吗?

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.

解决方法:

不幸的是,根据Doctrine Docs,您必须调用flush来获取外键的ID:

Generated entity identifiers / primary keys are guaranteed to be
available after the next successful flush operation that involves the
entity in question. You can not rely on a generated identifier to be
available directly after invoking persist. The inverse is also true.
You can not rely on a generated identifier being not available after a
Failed flush operation.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...