首页 > 解决方案 > 关联键上的多对一关联不起作用

问题描述

我在这个上下文的项目中使用 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>

但是这个映射是不正确的,似乎我们不能在拥有方的关联键上加入列。

我的错误是:

[FAIL] 实体类 App\Domain\Toto\TotoUser\Entity\Balance 映射无效:* 引用的列名 'id' 必须是目标实体类 'App\Domain\Toto\TotoUser 上的主键列\实体\TotoUser'。

关于如何解决这个问题的任何想法?

编辑:解决方案很简单:只需referenced-column-name在数据库中输入您的 ID 的实际名称...这里是user_id

标签: phpxmlsymfonydoctrine-ormdomain-driven-design

解决方案


在您的平衡模型中

referenced-column-name="securityUser"

我假设,“securityUser”不是你的 id ......这意味着这里是数据库中列的真实名称,通常命名为“id”


推荐阅读