微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐