Custom db namespaces with getCollectionGraph

Greetings. I need to return a table of entries, fetched akin to left join. My custom schema and tables are built with ExtraBuilder, and simple requests work like a charm. Raw SQL query works too, but i figured i’d rather do it the right way through classes and relations. After long search and trial, i’ve stumbled upon getCollectionGraph, which seemed like what i needed. But there is an issue i cannot resolve.

Here’s the part of schema for classes i’m working with:

<?xml version="1.0" encoding="UTF-8"?>
<model package="localDB\Model" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="InnoDB" phpdoc-package="" version="3.0">
  <object class="Equipment" table="our_equipment" extends="xPDO\Om\xPDOSimpleObject">
    <field key="name" dbtype="varchar" precision="50" phptype="string" null="true" default=""/>
    <field key="class_id" dbtype="int" precision="10" phptype="integer" null="false" default=""/>
    <field key="descr" dbtype="text" phptype="string" null="true" default=""/>
    <composite alias="Units" class="Stock" local="id" foreign="eqp_id" cardinality="many" owner="local"/>
  </object>
  <object class="Stock" table="our_stock" extends="xPDO\Om\xPDOSimpleObject">
    <field key="puid" dbtype="int" precision="10" phptype="integer" null="false" default=""/>
    <field key="eqp_id" dbtype="int" precision="10" phptype="integer" null="false" default=""/>
    <field key="decom_id" dbtype="int" precision="10" phptype="integer" null="true" default=""/>
    <field key="buh_id" dbtype="int" precision="10" phptype="integer" null="false" default=""/>
    <aggregate alias="Eqp" class="Equipment" local="eqp_id" foreign="id" cardinality="one" owner="foreign"/>
  </object>
</model>

And here’s the php code:

<?php
$output = '';
$namespace = "localDB\\Model\\";

$entries = $modx->getCollectionGraph($namespace.'Stock', '{"Eqp":{} }');

Which returns an empty result, with errorlog saying:

[2024-02-09 10:09:37] (ERROR @ C:\wamp64\www\wikia\core\vendor\xpdo\xpdo\src\xPDO\xPDO.php : 666) Could not load class: Equipment from mysql.equipment
[2024-02-09 10:09:37] (ERROR @ C:\wamp64\www\wikia\core\vendor\xpdo\xpdo\src\xPDO\xPDO.php : 786) Equipment::getSelectColumns() is not a valid static method.
[2024-02-09 10:09:37] (ERROR @ C:\wamp64\www\wikia\core\vendor\xpdo\xpdo\src\xPDO\xPDO.php : 666) Could not load class: Equipment from mysql.equipment
[2024-02-09 10:09:37] (ERROR @ C:\wamp64\www\wikia\core\vendor\xpdo\xpdo\src\xPDO\Om\xPDOObject.php : 534) Error 42S22 executing query: SELECT `Stock`.`id` AS `Stock_id`, `Stock`.`puid` AS `Stock_puid`, `Stock`.`eqp_id` AS `Stock_eqp_id`, `Stock`.`decom_id` AS `Stock_decom_id`, `Stock`.`buh_id` AS `Stock_buh_id`, `` FROM `qx_our_stock` AS `Stock` ORDER BY `Stock`.`id` ASC  - Array
(
    [0] => 42S22
    [1] => 1054
    [2] => Unknown column '' in 'field list'
)

I’ve tried adding $namespace before the Eqp like that:

$entries = $modx->getCollectionGraph($namespace.'Stock', 
'{'.$namespace.'"Eqp":{} }'
);

Then the Stock part loads without errors, but Eqp still returns an error “Could not load class: Equipment from mysql.equipment”. Which is, i suppose, because in relation the namespace is not defined and it fails to find the class. Question is - how to define a namespace correctly? I am using namespace variable by following this tutorial , how to get rid of it?

In MODX 3 schemas, use the fully qualified class name for relationships:

<aggregate alias="Eqp" class="localDB\Model\Equipment" local="eqp_id" foreign="id" cardinality="one" owner="foreign"/>

Oh, that simple xD Thanks, that worked like a charm

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.