php - 关联键上的多对一关联不起作用
问题描述
我在这个上下文的项目中使用 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 上的主键列\实体\TotoUser'。
关于如何解决这个问题的任何想法?
编辑:解决方案很简单:只需referenced-column-name
在数据库中输入您的 ID 的实际名称...这里是user_id
解决方案
在您的平衡模型中
referenced-column-name="securityUser"
我假设,“securityUser”不是你的 id ......这意味着这里是数据库中列的真实名称,通常命名为“id”
推荐阅读
- javascript - 在 jsImageBox 中单击内容时关闭弹出窗口
- android - RecyclerView 与 Horizontal GridLayoutManager 和 PageIndicator
- python-3.x - 如何在课堂上记忆?
- docker - 通知 Docker 容器重启的 Alert Manager 警报规则是什么?
- android - 微调器文本大小不变(以编程方式)
- javascript - 如何键入类扩展,以便类的构造函数接受预期类型
- java - Spring Boot 执行器和 Log4j2
- python - ModuleNotFoundError:没有名为“pip.commands”的模块
- php - 通过没有密码的 php-deployer 部署后重新加载 php-fpm
- sql-server - Sql server 查询方括号内的数字