Greco Jonathan
Greco Jonathan

Reputation: 2544

ManyToOne association on association key not work

I'm using DDD on my project with this contexts (I've anonymise one context by Toto) :

My Toto Context have a balance system (currency debits & credits) In Toto I've a TotoUser table that refers by it's associated key the securityUser as follow :

<?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>

In this TotoUser table I've a collection of transactions, each transaction is a balance entry (in debit or credit)

My balance table has this mapping

<?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>

But this mapping is incorrect, it seems that we can't join column on an associated key in the Owning Side.

My errors are :

[FAIL] The entity-class App\Domain\Toto\TotoUser\Entity\Balance mapping is invalid: * The referenced column name 'id' has to be a primary key column on the target entity class 'App\Domain\Toto\TotoUser\Entity\TotoUser'.

Any ideas on how to solve this ?

EDIT : The solution is simple: just put in referenced-column-name the actual name of your ID in DB...here is user_id

Upvotes: 0

Views: 251

Answers (1)

Jim Panse
Jim Panse

Reputation: 2270

In your Balance model

referenced-column-name="securityUser"

I assume, that "securityUser" is not your id ... meant here is the real name of the column in the database, usually named "id"

Upvotes: 2

Related Questions