Join 2 tables in migx

Hello all. I have a following schema.

<object class="localizatorContent" table="localizator_content" extends="xPDOSimpleObject">
              <field key="resource_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="true" default="0"/>
              <field key="key" dbtype="varchar" precision="100" phptype="string" null="false" default=""/>
              <field key="pagetitle" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              <field key="longtitle" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              <field key="menutitle" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              <field key="seotitle" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              <field key="keywords" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              <field key="introtext" dbtype="text" phptype="string" null="true" default=""/>
              <field key="description" dbtype="text" phptype="string" null="true" default=""/>
              <field key="content" dbtype="mediumtext" phptype="string" null="true"  />
              <field key="active" dbtype="tinyint" precision="1" phptype="boolean" null="true" default="1"/>

              <index alias="resource_id" name="resource_id" primary="false" unique="false" type="BTREE">
                     <column key="resource_id" length="" collation="A" null="false"/>
              </index>
              <index alias="key" name="key" primary="false" unique="false" type="BTREE">
                     <column key="key" length="" collation="A" null="false"/>
              </index>

              <aggregate alias="Resource" class="modResource" local="resource_id" foreign="id" cardinality="one" owner="foreign" />
       </object>

Now is what I want is to join this table with modResource using migx loop collection. Here is what I get so far

<ul>
  [[migxLoopCollection?
  &classname = `modResource`
  &debug="1"
  &joins=`[{"alias":"Resource","classname":"localizatorContent","on":"localizatorContent.Resource=modResource.id"}]`
  
  ]]
</ul>

After all I get a following error:

Fatal error : Uncaught Error: Call to a member function get() on null in /Users/bogdanonisenko/Applications/multilang/core/components/localizator/model/localizator/localizatorcontent.class.php:70

So how to do a join correctly?

try it like that:

  [[migxLoopCollection?
  &packageName=`yourpackagenamegoeshere`
  &classname = `modResource`
  &debug=`1`
 &joins=`[{"alias":"Localizator","classname":"localizatorContent","on":"Localizator.resource_id=modResource.id"}]`
  
  ]]
1 Like

Where can I get a package name from? And what is it?

From your schema:

<?xml version="1.0" encoding="UTF-8"?>
<model package="yourpackagenamegoeshere" ...>
1 Like

didn’t you create a package under core/components/ where you have your schema?

Sorry I did. My problem is that I try to rewrite modul from version 2 to 3. Some of it working…

But adding package name didn’t help me. I get the same error.

May be it would be helpfull if I post the whole schema:

<?xml version="1.0" encoding="UTF-8"?>
<model package="localizator" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" phpdoc-package="localizator"
       version="1.1">

       <object class="localizatorLanguage" table="localizator_languages" extends="xPDOSimpleObject">
              <field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default=""/>
              <field key="key" dbtype="varchar" precision="100" phptype="string" null="false" default=""/>
              <field key="cultureKey" dbtype="varchar" precision="100" phptype="string" null="true" default=""/>
              <field key="http_host" dbtype="varchar" precision="100" phptype="string" null="false" default=""/>
              <field key="description" dbtype="text" phptype="string" null="true" default=""/>
              <field key="active" dbtype="tinyint" precision="1" phptype="boolean" null="true" default="1"/>

              <index alias="key" name="key" primary="false" unique="false" type="BTREE">
                     <column key="key" length="" collation="A" null="false"/>
              </index>
              <index alias="cultureKey" name="cultureKey" primary="false" unique="false" type="BTREE">
                     <column key="cultureKey" length="" collation="A" null="true"/>
              </index>
       </object>

       <object class="localizatorContent" table="localizator_content" extends="xPDOSimpleObject">
              <field key="resource_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="true" default="0"/>
              <field key="key" dbtype="varchar" precision="100" phptype="string" null="false" default=""/>
              <field key="pagetitle" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              <field key="longtitle" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              <field key="menutitle" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              <field key="seotitle" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              <field key="keywords" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
              <field key="introtext" dbtype="text" phptype="string" null="true" default=""/>
              <field key="description" dbtype="text" phptype="string" null="true" default=""/>
              <field key="content" dbtype="mediumtext" phptype="string" null="true"  />
              <field key="active" dbtype="tinyint" precision="1" phptype="boolean" null="true" default="1"/>

              <index alias="resource_id" name="resource_id" primary="false" unique="false" type="BTREE">
                     <column key="resource_id" length="" collation="A" null="false"/>
              </index>
              <index alias="key" name="key" primary="false" unique="false" type="BTREE">
                     <column key="key" length="" collation="A" null="false"/>
              </index>

              <aggregate alias="Resource" class="modResource" local="resource_id" foreign="id" cardinality="one" owner="foreign" />
       </object>

       <object class="locTemplateVarResource" table="localizator_tmplvar_contentvalues" extends="xPDOSimpleObject">
              <field key="key" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index"/>
              <field key="tmplvarid" dbtype="int" precision="10" phptype="integer" null="false" default="0" index="index" />
              <field key="contentid" dbtype="int" precision="10" phptype="integer" null="false" default="0" index="index" />
              <field key="value" dbtype="mediumtext" phptype="string" null="false" />

              <index alias="key" name="key" primary="false" unique="false" type="BTREE">
                     <column key="key" length="" collation="A" null="false" />
              </index>
              <index alias="tmplvarid" name="tmplvarid" primary="false" unique="false" type="BTREE">
                     <column key="tmplvarid" length="" collation="A" null="false" />
              </index>
              <index alias="contentid" name="contentid" primary="false" unique="false" type="BTREE">
                     <column key="contentid" length="" collation="A" null="false" />
              </index>
              <index alias="tv_cnt" name="tv_cnt" primary="false" unique="true" type="BTREE">
                     <column key="key" length="" collation="A" null="false" />
                     <column key="tmplvarid" length="" collation="A" null="false" />
                     <column key="contentid" length="" collation="A" null="false" />
              </index>

              <aggregate alias="TemplateVar" class="modTemplateVar" local="tmplvarid" foreign="id" cardinality="one" owner="foreign" />
              <aggregate alias="Resource" class="modResource" local="contentid" foreign="id" cardinality="one" owner="foreign" />
       </object>

</model>

Does it work if you try to access your custom classes in a snippet with $modx->addPackage(...); and $modx->getCollection('localizatorContent');?


What exactly do you mean by that?

Most things should still work the same in MODX3.

Yes I am able to get an object. I use a custom snippets to show the data.

Hmm I can get object in snippet like this

$page = $modx->getObject('localizatorContent', ['key' => $key,'resource_id'=>$id]);

There I am passing the parameters to that function. But in this case I need to get all data from the table. Originaly I didn’t think that Migx will use that function. Will look at it now. An other question is is there anythin like query builder in Laravel or Yii or any other ORM?

Maybe just join the tables yourself then, without using migxLoopCollection.


I don’t think so.

1 Like

did you parse your schema on MODX 2 or on MODX 3?

MIGXdb currently can only handle packages generated from MODX 2, but should then work on MODX 3