我找到了如何使用属性来做到这一点https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/tutorials/composite-primary-keys.html#use-case-2-simple -衍生身份,但是有没有办法对 XML 做同样的事情?
我尝试过这样
<id name="user_id" type="string" />
<one-to-one field="product" target-entity="App\Entity\User" orphan-removal="true">
<cascade>
<cascade-persist/>
<cascade-remove/>
</cascade>
<join-column name="user_id" referenced-column-name="id" on-delete="CASCADE" />
</one-to-one>
并且失败并出现错误
Property App\Entity\Address::$user_id does not exist
我也这样尝试过
<one-to-one field="product" target-entity="App\Entity\User" orphan-removal="true">
<cascade>
<cascade-persist/>
<cascade-remove/>
</cascade>
<id name="user_id" type="string" />
<join-column name="user_id" referenced-column-name="id" on-delete="CASCADE" />
</one-to-one>
错误是
No identifier/primary key specified for Entity "App\Entity\Address". Every Entity must have an identifier/primary key.
我找到了解决方案,关键是添加
association-key="true"
,就像这样
<id name="user" association-key="true" />
<one-to-one field="product" target-entity="App\Entity\User" orphan-removal="true">
<cascade>
<cascade-persist/>
<cascade-remove/>
</cascade>
<join-column name="user_id" referenced-column-name="id" on-delete="CASCADE" />
</one-to-one>