【发布时间】:2012-02-25 09:31:06
【问题描述】:
我有三个 Doctrine 实体:Device,它与 Device\Status 具有 OneToOne 关系,它又与 Device\Status\Battery 具有 OneToOne 关系。
我在相关实体之间设置了 {cascade="persist"},根据我的阅读,这应该是 Doctrine 自动持久化每个实体所需的全部内容,而无需我自己在代码。
这是我遇到的问题:
$device = new \Entities\Device();
$device->setId(100);
$status = $device->getStatus();
$status->setIpAddress('192.168.0.1');
$battery = $status->getBattery();
$battery->setInternalLevel(60);
$em->persist($device);
$em->flush();
执行此代码后,我收到以下错误:
Entity of type Device\Status\Battery has identity through a foreign entity
Device\Status, 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 'Device\Status\Battery'. 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.
我的问题是:设置我的实体以确保它们以正确的顺序保留的正确方法是什么?
实体的代码可以在这里找到:https://gist.github.com/1753524
所有测试均使用 Doctrine 2.2 沙箱进行。
【问题讨论】:
-
我遇到了几乎同样的问题。你必须在每个持久化之间调用flush。
-
@CappY 根据 Doctrine 文档,由于我为每个实体设置了 {cascade="persist"},我不需要手动持久化每个实体。此代码应该按原样工作。 readthedocs.org/docs/doctrine-orm/en/latest/reference/…
-
请提供short, self contained, correct example。您的代码在尝试将值直接分配给
protected属性时触发访问冲突,即$device->id = 100 -
如果在设置了每个属性之后你也设置了反面呢?像这样:在
$status->device = $device;之后执行$device->status = $status;,然后在$battery->status = $status;之后执行$status->battery = $battery; -
@Phil 完成,谢谢提醒。
标签: php orm doctrine mapping doctrine-orm