php持久对象,php-在理论中具有两个外来身份的持久对象
我已经在我的symfony包的Resources / 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-在理论中具有两个外来身份的持久对象相关推荐
- java memcached 存储对象_java – 从Memcache中获取低级别数据存储区实体对象时的慢速反序列化...
事实证明,检索存储内存缓存的低级数据存储实体非常缓慢.由于objectify将实体缓存为低级数据存储区实体类型,因此当使用objectify从memcache中获取许多实体时,这会导致性能不佳. 真正 ...
- java复制两个对象报异常_Java中复制两个不同类的对象的属性
Apache的开源包BeanUtils用来复制两个对象的属性,要使用这个Apache的开源包,要先去官网下载commons-beanutils-1.X.jar包,http://www.apache.o ...
- 《随机过程》布朗运动理论中的两个反常问题
全世界只有3.14 % 的人关注了 爆炸吧知识 1827 年,英国植物学家布朗(Brown)用显微镜观察悬浮在液体中的花粉微粒时,发现花粉微粒总是在做无规则运动.后来人们发现,这是一种广泛存在于自然界 ...
- 合并两个对象 java_在Java中合并两个对象列表8
如果要实现equals和hashCode,那么这个方法就在Parent类中.在该类中添加类似的方法 @Override public int hashCode() { return Objects.h ...
- 在python中函数和类都属于可调用对象_在Python中函数和类都属于可调用对象
根据空气流动的动力不同,通气方式可分为()两种. 常见往复泵的排出压力低,是何原因? 影响定价的因素主要包括定价目标.产品成本.市场需求和竞争者等四个因素. 什么是排水系统? 用质量为0.25kg的锤 ...
- android json字符串转成json对象_在PHP中处理JSON数组以及对象
php中文网最新课程 每日17点准时技术干货分享 在 PHP 中处理 JSON 数组以及对象 与客户端混淆的常见原因是围绕 JSON 数组和对象,以及如何在 PHP 中指定他们.特别是,问题是由空对象 ...
- JS 删除对象(Object)中的键值对
JS 删除对象(Object)中的键值对 假设有以下对象 const person = {name: 'zhangsan',age: 25,gender: 'man' } 可使用以下语句删除年龄age ...
- php json输出对象的属性值,JavaScript_jquery动态遍历Json对象的属性和值的方法,1、遍历 json 对象的属性/ - phpStudy...
jquery动态遍历Json对象的属性和值的方法 1.遍历 json 对象的属性 //定义json对象 var person= { name: 'zhangsan', pass: '123', fn: ...
- php对象持久化,在 Oracle 中完成 PHP5 对象的持久
在 Oracle 中完成 PHP5 对象的持久 作者:Barry McKay 在数据库驱动的 Web 应用程序中实现 PHP5 对象的持久,朝着完全面向对象的 Web 应用程序开发迈进重要的一步. 2 ...
最新文章
- 007_Buzz事件
- 替换UI--遮挡问题
- CSU1323: ZZY and his little friends
- 每天一道LeetCode-----摩尔投票法寻找给定数组中出现个数大于n/2或n/3的元素
- 2字节取值范围_Java注解-元数据、注解分类、内置注解和自定义注解|乐字节
- k8s高可用集群_搭建高可用集群(部署haproxy和安装docker以及其他组件)---K8S_Google工作笔记0056
- winscp自动执行脚本
- java 编程思想 一 第二章(对象)
- python爬虫简历项目怎么写_python爬虫简历
- Django ORM 使用手册
- 计算机与三菱plc485通讯,三菱plc同三菱变频器RS-485通讯功能的编程实例
- 【GAPPER乡村笔记项目】盘点老龄化社会背景下人工智能及机器人技术的应用
- AXI协议(5):AXI协议的burst机制
- 排列组合 C语言函数,排列组合(C递归版)
- [ 代码审计篇 ] 代码审计思路 详解
- 您知道Linux下C语言编程的一些注意事项吗_教育中国
- Oracle 19c 安装步骤(超详细)
- 用html做一个15页的页面,html西式甜品网制作(15页)-原创力文档
- 如何微调Chinese-Vicuna-7b模型
- 为什么很多人跨学科也要转行模拟版图设计工程师?