我在具有这种上下文的项目上使用DDD(我已经用Toto匿名化了一个上下文):
我的Toto上下文具有余额系统(货币借方和贷方) 在Toto中,我有一个TotoUser表,由它的关联密钥引用securityUser,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="App\Domain\Toto\TotoUser\Entity\TotoUser" table="dol_toto_users">
<id name="securityUser" association-key="true"/>
<one-to-one field="securityUser" target-entity="App\Domain\UAC\User\Entity\User">
<join-columns>
<join-column name="user_id"/>
</join-columns>
</one-to-one>
<one-to-many field="transactions" target-entity="App\Domain\Toto\TotoUser\Entity\Balance" mapped-by="user">
<order-by>
<order-by-field name="date" direction="DESC"/>
</order-by>
<cascade>
<cascade-remove/>
<cascade-persist/>
</cascade>
</one-to-many>
<field name="currentCurrency" type="float">
<options>
<option name="comment">Current amount of currency owned by the user</option>
</options>
</field>
</entity>
</doctrine-mapping>
在此TotoUser表中,我有一个交易集合,每个交易都是余额项(借方或贷方)
我的余额表具有此映射
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity repository-class="App\Api\Template\Repository\BalanceRepository" name="App\Domain\Toto\TotoUser\Entity\Balance" table="dol_balance">
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="amount" type="decimal" column="amount" precision="15" scale="2" nullable="false"/>
<field name="date" type="datetime" column="date" precision="0" scale="0" nullable="false"/>
<many-to-one field="user" target-entity="App\Domain\Toto\TotoUser\Entity\TotoUser" inversed-by="transactions">
<join-column name="user_id" referenced-column-name="securityUser"/>
</many-to-one>
<many-to-one field="type" target-entity="App\Domain\Toto\Template\Entity\Type"/>
</entity>
</doctrine-mapping>
但是这种映射是不正确的,似乎我们无法在所有者侧的关联键上加入列。
我的错误是:
在“ App \ Domain \ Toto \ TotoUser \ Entity \ TotoUser”上找不到字段 映射到列“ id”。该字段不存在或 关联存在,但是有多个联接列。
和一个学说:模式:验证:
[FAIL]实体类App \ Domain \ Toto \ TotoUser \ Entity \ Balance 映射无效:*引用的列名'id'必须为 目标实体类上的主键列 “ App \ Domain \ Toto \ TotoUser \ Entity \ TotoUser”。
关于如何解决此问题的任何想法?
编辑:解决方案很简单:只需在数据库中输入referenced-column-name
ID的实际名称...这里是user_id
答案 0 :(得分:2)
在您的余额模型中
referenced-column-name="securityUser"
我认为,“ securityUser”不是您的ID……这是数据库中列的真实名称,通常名为“ ID”