我有三个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();
Run Code Online (Sandbox Code Playgroud)
执行此代码后,我收到以下错误:
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 …
Run Code Online (Sandbox Code Playgroud)