我有三个Doctrine实体:Device,它与Device \ Status具有OneToOne关系,而Device \ Status与Device \ Status \ Battery具有OneToOne关系。Doctrine 2多级OneToOne级联
我在相关实体之间设置了{cascade =“persist”},并且从我所读到的内容来看,这应该是所有为自动坚持每个实体所需的全部内容,而无需自己做任何事情代码。
这里是我有什么用的问题:
$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.
我的问题是:什么是正确的方法来设置我的实体以确保它们保持正确的顺序?
为实体的代码可以在这里找到:使用学说2.2沙箱已经进行https://gist.github.com/1753524
所有测试。
我有几乎相同的问题。你必须在每次持续之间调用flush。 – CappY 2012-02-05 18:03:11
@CappY根据Doctrine文档,因为我为每个实体设置了{cascade =“persist”},所以我不需要手动持久化每个实体。此代码*应该*按原样工作。 http://readthedocs.org/docs/doctrine-orm/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations – Taeram 2012-02-06 04:10:21
请提供一个[简短,自包含,正确的示例](http ://sscce.org/)。当你尝试将值直接赋值给'protected'属性时,你的代码触发访问冲突,即'$ device-> id = 100' – Phil 2012-02-06 05:08:00