我已经在我的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.

标签:doctrine-orm,symfony,php,database

来源: https://codeday.me/bug/20191030/1968810.html

php持久对象,php-在理论中具有两个外来身份的持久对象相关推荐

  1. java memcached 存储对象_java – 从Memcache中获取低级别数据存储区实体对象时的慢速反序列化...

    事实证明,检索存储内存缓存的低级数据存储实体非常缓慢.由于objectify将实体缓存为低级数据存储区实体类型,因此当使用objectify从memcache中获取许多实体时,这会导致性能不佳. 真正 ...

  2. java复制两个对象报异常_Java中复制两个不同类的对象的属性

    Apache的开源包BeanUtils用来复制两个对象的属性,要使用这个Apache的开源包,要先去官网下载commons-beanutils-1.X.jar包,http://www.apache.o ...

  3. 《随机过程》布朗运动理论中的两个反常问题

    全世界只有3.14 % 的人关注了 爆炸吧知识 1827 年,英国植物学家布朗(Brown)用显微镜观察悬浮在液体中的花粉微粒时,发现花粉微粒总是在做无规则运动.后来人们发现,这是一种广泛存在于自然界 ...

  4. 合并两个对象 java_在Java中合并两个对象列表8

    如果要实现equals和hashCode,那么这个方法就在Parent类中.在该类中添加类似的方法 @Override public int hashCode() { return Objects.h ...

  5. 在python中函数和类都属于可调用对象_在Python中函数和类都属于可调用对象

    根据空气流动的动力不同,通气方式可分为()两种. 常见往复泵的排出压力低,是何原因? 影响定价的因素主要包括定价目标.产品成本.市场需求和竞争者等四个因素. 什么是排水系统? 用质量为0.25kg的锤 ...

  6. android json字符串转成json对象_在PHP中处理JSON数组以及对象

    php中文网最新课程 每日17点准时技术干货分享 在 PHP 中处理 JSON 数组以及对象 与客户端混淆的常见原因是围绕 JSON 数组和对象,以及如何在 PHP 中指定他们.特别是,问题是由空对象 ...

  7. JS 删除对象(Object)中的键值对

    JS 删除对象(Object)中的键值对 假设有以下对象 const person = {name: 'zhangsan',age: 25,gender: 'man' } 可使用以下语句删除年龄age ...

  8. php json输出对象的属性值,JavaScript_jquery动态遍历Json对象的属性和值的方法,1、遍历 json 对象的属性/ - phpStudy...

    jquery动态遍历Json对象的属性和值的方法 1.遍历 json 对象的属性 //定义json对象 var person= { name: 'zhangsan', pass: '123', fn: ...

  9. php对象持久化,在 Oracle 中完成 PHP5 对象的持久

    在 Oracle 中完成 PHP5 对象的持久 作者:Barry McKay 在数据库驱动的 Web 应用程序中实现 PHP5 对象的持久,朝着完全面向对象的 Web 应用程序开发迈进重要的一步. 2 ...

最新文章

  1. 007_Buzz事件
  2. 替换UI--遮挡问题
  3. CSU1323: ZZY and his little friends
  4. 每天一道LeetCode-----摩尔投票法寻找给定数组中出现个数大于n/2或n/3的元素
  5. 2字节取值范围_Java注解-元数据、注解分类、内置注解和自定义注解|乐字节
  6. k8s高可用集群_搭建高可用集群(部署haproxy和安装docker以及其他组件)---K8S_Google工作笔记0056
  7. winscp自动执行脚本
  8. java 编程思想 一 第二章(对象)
  9. python爬虫简历项目怎么写_python爬虫简历
  10. Django ORM 使用手册
  11. 计算机与三菱plc485通讯,三菱plc同三菱变频器RS-485通讯功能的编程实例
  12. 【GAPPER乡村笔记项目】盘点老龄化社会背景下人工智能及机器人技术的应用
  13. AXI协议(5):AXI协议的burst机制
  14. 排列组合 C语言函数,排列组合(C递归版)
  15. [ 代码审计篇 ] 代码审计思路 详解
  16. 您知道Linux下C语言编程的一些注意事项吗_教育中国
  17. Oracle 19c 安装步骤(超详细)
  18. 用html做一个15页的页面,html西式甜品网制作(15页)-原创力文档
  19. 如何微调Chinese-Vicuna-7b模型
  20. 为什么很多人跨学科也要转行模拟版图设计工程师?

热门文章

  1. 叮咚!7.24运维节,您有一份礼物待查收!
  2. 不会SQL注入,连漫画都看不懂了
  3. 2019年5月数据库流行度排行:老骥伏枥与青壮图强
  4. 无路可逃:Oracle 12.2 BigSCN新特性可能的DB Link兼容性问题
  5. 从0到1 | 滴滴DB自动化运维实践了解一下
  6. 偷梁换柱 | 无备份情况下的数据恢复实践(二)
  7. 在Spark Scala/Java应用中调用Python脚本,会么?
  8. NB-IoT四大关键特性及实现告诉你,为啥NB
  9. IAP:物联网终端软件升级技术
  10. 基于DAYU的实时作业开发,分分钟搭建企业个性化推荐平台