I am running XPDO v3 outside of modx and created a schema with some related tables.
When I query the main table and try to get the related table items, it only returns the last one. Instead of all the items.
This is (part of) my schema:
<object class="MiTeams" table="mi_teams" extends="xPDO\Om\xPDOObject">
<field key="ID" dbtype="int" precision="20" phptype="integer" null="false" index="pk" generated="native"/>
<field key="date" dbtype="datetime" phptype="datetime" null="true"/>
<field key="title" dbtype="varchar" precision="255" phptype="string" null="false"/>
<field key="created_by" dbtype="tinyint" precision="4" phptype="int" null="true"/>
<field key="created_on" dbtype="datetime" phptype="datetime" null="true"/>
<field key="modified_by" dbtype="tinyint" precision="4" phptype="int" null="true"/>
<field key="modified_on" dbtype="datetime" phptype="datetime" null="true"/>
<field key="done" dbtype="tinyint" precision="1" phptype="int" default="0"/>
<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE">
<column key="ID" length="" collation="A" null="false"/>
</index>
<composite alias="MiTeamTask" class="MiApi\Model\MiTeamTasks" local="ID" foreign="team_id" cardinality="many" owner="local"/>
<composite alias="MiTeamUser" class="MiApi\Model\MiTeamUsers" local="ID" foreign="team_id" cardinality="many" owner="local"/>
<composite alias="MiTeamBus" class="MiApi\Model\MiTeamBusses" local="ID" foreign="team_id" cardinality="many" owner="local"/>
</object>
<object class="MiTeamTasks" table="mi_team_tasks" extends="xPDO\Om\xPDOObject">
<field key="team_id" dbtype="int" phptype="int" null="false" index="index"/>
<field key="task_id" dbtype="int" phptype="int" default="0" null="false"/>
<field key="task_type" dbtype="varchar" precision="255" phptype="string" null="false"/>
<field key="contract_id" dbtype="int" precision="10" phptype="int" null="false"/>
<field key="client" dbtype="varchar" precision="255" phptype="string" null="false"/>
<field key="address" dbtype="varchar" precision="255" phptype="string" null="false"/>
<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE">
<column key="team_id" length="" collation="A" null="false"/>
</index>
<aggregate alias="MiTeam" class="MiApi\Model\MiTeams" local="team_id" foreign="ID" cardinality="one" owner="foreign"/>
</object>
<object class="MiTeamUsers" table="mi_team_users" extends="xPDO\Om\xPDOObject">
<field key="team_id" dbtype="int" phptype="int" null="false" index="index"/>
<field key="user_id" dbtype="tinyint" precision="100" phptype="int" default="0"/>
<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE">
<column key="team_id" length="" collation="A" null="false"/>
</index>
<aggregate alias="MiTeam" class="MiApi\Model\MiTeams" local="team_id" foreign="ID" cardinality="one" owner="foreign"/>
</object>
<object class="MiTeamBusses" table="mi_team_busses" extends="xPDO\Om\xPDOObject">
<field key="team_id" dbtype="int" phptype="int" null="false" index="index"/>
<field key="bus_id" dbtype="tinyint" precision="100" phptype="int" default="0"/>
<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE">
<column key="team_id" length="" collation="A" null="false"/>
</index>
<aggregate alias="MiTeam" class="MiApi\Model\MiTeams" local="team_id" foreign="ID" cardinality="one" owner="foreign"/>
</object>
And this is my code to get the teams and the Tasks:
$teamsClass = MiTeams::class;
$c = $this->xpdo->newQuery($teamsClass);
//$c->where(['date:LIKE' => $date.'%']);
$teams = $this->xpdo->getCollectionGraph($teamsClass, '{"MiTeamTask":{}}', $c);
foreach ($teams as $team) {
$tasks = $team->getMany('MiTeamTask');
foreach ($tasks as $task) {
print_r($task->toArray());
}
}
This in my MiTeams table:
insert into mi_api_1.mi_teams (ID, date, title, created_by, created_on, modified_by, modified_on, done)
values (1, '2023-01-03 10:46:30', 'test team', 22, '2023-01-03 10:46:44', null, null, 0);
I have 2 tasks in MiTeamTasks:
insert into mi_api_1.mi_team_tasks (team_id, task_id, task_type, contract_id, client, address)
values (1, 2, 'Plaatsen', 122345, 'company one', 'street one 14'),
(1, 4, 'Ophalen', 122346, 'berdijf 2', 'straat 2');
The result of the print_r is this:
Array
(
[team_id] => 1
[task_id] => 4
[task_type] => Ophalen
[contract_id] => 122346
[client] => berdijf 2
[address] => straat 2
)
As you can see its only the last one.
I do not know what I did wrong, but I must have missed somthing in my schema I think…
When I set the 2nd task to team_id 2 then I see the first one.
Hope someone can help me out with this problem…