We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10696
    • 35 Posts
    Hello, everybody!

    I have a simplest schema:

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

    <object class="puCategory" table="pu_categories" extends="xPDOSimpleObject">
    <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>

    <composite alias="items" class="puItem" local="id" foreign="category" cardinality="many" owner="local"/>
    </object>

    <object class="puItem" table="pu_items" extends="xPDOSimpleObject">
    <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>

    <aggregate alias="category" class="puCategory" local="category" foreign="id" cardinality="one" owner="foreign"/>
    </object>

    </model>


    As you can see this is One-to-Many relationship.
    I’ve created a couple categories via snippet like
    <?php
      $modx->getService('priceupdater','PriceUpdater','path/to/component/');
      // assuming service got successfull
      $c = $modx->newObject('puCategory',array('name'=>'Some name'));
      $c->save();
      // and copy-paste several times
    ?>


    Then I dynamically added to each category some items:
    <?php
      for($i = 0; $i < 3; $i++){
        $c = $modx->getObject('puCategory',$i);
        $items = array();
        for($j = 0; $j < 10; $j++){
          $item = $modx->newObject('puItem',array('name'=>'Item'.$j));
          $items[] = $item;
        }
        $c->addMany($items);
        $c->save();
      }
    ?>


    All new items, that was created by this code are saved, but when I try to get all items of some category I’m getting NULL!
    <?php
      $c = $modx->getObject('puCategory',1);
      $i = $c->getMany('items');
      echo ($i == null)? 'NULL' : 'some items';
      // this will return NULL
    ?>


    Is there an error in my schema? Or maybe it’s a bug?

    I’m running MODx Revolution 2.0.8-pl (traditional)
      • 22303 MODX Staff
      • 10,725 Posts
      The foreign attribute on the items composite refers to a column ’category’ that does not exist in your schema for puItem. Likewise, this field is referred to as local on the category aggregate relation in puItem. That must refer to a valid field in the related class.
        • 10696
        • 35 Posts
        Quote from: OpenGeek at May 13, 2011, 09:34 AM

        The foreign attribute on the items composite refers to a column ’category’ that does not exist in your schema for puItem. Likewise, this field is referred to as local on the category aggregate relation in puItem. That must refer to a valid field in the related class.

        Hmmm... strange. Is this means that this example is wrong?
          • 10696
          • 35 Posts
          Yes, the example is incorrect.

          Solved by adding to puItem field "category":

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

          <object class="puCategory" table="pu_categories" extends="xPDOSimpleObject">
          <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
          <composite alias="items" class="puItem" local="id" foreign="category" cardinality="many" owner="local"/>
          </object>

          <object class="puItem" table="pu_items" extends="xPDOSimpleObject">
          <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
          <field key="category" dbtype="int" precision="11" attributes="unsigned" phptype="integer" null="true"/>
          <aggregate alias="category" class="puCategory" local="category" foreign="id" cardinality="one" owner="foreign"/>
          </object>

          </model>
            • 18373 ☆ A M B ☆
            • 3,141 Posts
            Thanks, updated the docs.
              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.