On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • I'm having trouble with a mapping table. When I call getMany(), it does return the expected number of items. But when I iterate over those items to get the name, it returns nothing. Here is an excerpt for my schema file for the mapped tables:

    	<object class="rtPositionCategoriesMap" table="recroot_position_categories_map" extends="xPDOSimpleObject">
    		<field key="position_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" index="index" />
    		<field key="category_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" index="index" />
    		<index alias="category" name="category" primary="false" unique="false" type="BTREE" >
    			<column key="category_id" length="" collation="A" null="false" />
    		</index>
    		<index alias="position" name="position" primary="false" unique="false" type="BTREE" >
    			<column key="position_id" length="" collation="A" null="false" />
    		</index>
    		<aggregate alias="Position" class="rtPositions" local="position_id" foreign="position_id" cardinality="one" owner="foreign" />
    		<aggregate alias="Category" class="rtPositionCategories" local="category_id" foreign="positioncategory_id" cardinality="one" owner="foreign" />
    
    	</object>
    
    	<object class="rtPositionCategories" table="recroot_positioncategories" extends="xPDOObject">
    		<field key="positioncategory_id" dbtype="int" precision="11" phptype="integer" null="false" index="pk"  generated="native" />
    		<field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    
    		<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
    			<column key="positioncategory_id" length="" collation="A" null="false" />
    		</index>
    		<composite alias="PositionCategories" class="rtPositionCategoriesMap" local="positioncategory_id" foreign="category_id" cardinality="many" owner="local" />
    	</object>
    	<object class="rtPositions" table="recroot_positions" extends="xPDOObject">
    		<field key="position_id" dbtype="int" precision="11" phptype="integer" null="false" index="pk"  generated="native" />
    		<field key="title" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    		<field key="location" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    		<field key="company" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
    		<field key="type" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
    		<field key="source" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
    		<field key="description" dbtype="text" phptype="string" null="false" />
    		<field key="applylink" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    		<field key="expireson" dbtype="datetime" phptype="datetime" null="true" />
    		<field key="active" dbtype="int" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" index="index" />
    		<field key="featured" dbtype="int" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" index="index" />
    		<field key="filled" dbtype="int" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" index="index" />
    
    		<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
    			<column key="position_id" length="" collation="A" null="false" />
    		</index>
    		<index alias="active" name="active" primary="false" unique="false" type="BTREE" >
    			<column key="active" length="" collation="A" null="false" />
    		</index>
    		<index alias="featured" name="featured" primary="false" unique="false" type="BTREE" >
    			<column key="featured" length="" collation="A" null="false" />
    		</index>
    		<index alias="filled" name="filled" primary="false" unique="false" type="BTREE" >
    			<column key="filled" length="" collation="A" null="false" />
    		</index>
    		<aggregate alias="Type" class="rtPositionTypes" local="type" foreign="positiontype_id" cardinality="one" owner="foreign" />
    		<aggregate alias="Company" class="rtCompanies" local="company" foreign="company_id" cardinality="one" owner="foreign" />
    		<aggregate alias="Source" class="rtSources" local="source" foreign="source_id" cardinality="one" owner="foreign" />
    		<composite alias="PositionCategories" class="rtPositionCategoriesMap" local="position_id" foreign="position_id" cardinality="many" owner="local" />
    	</object>
    


    I've been over this so many times I think I'm overlooking something silly. Can anyone see an issue with this?

    My snippet call is:
        //get category info
        $pcategories = $position->getMany('PositionCategories');
        $length = count($pcategories);
        error_log("Name: " . $this_title . " Count: " . $length);
        foreach ($pcategories as $pcategory) {
           $this_name = $pcategory->get('name');
           $thisCategoryNames .= $this_name . " ";
           array_push($positionClasses,$this_name);
        }
        error_log("\tCategories Found :" . $thisCategoryNames);//<-- comes back empty
    
    


    TIA.

    This question has been answered by Bruno17. See the first response.

    • discuss.answer
      well, with getMany, you get the rtPositionCategoriesMap - items, but not the related
      rtPositionCategories - items.

      you could either create a query with a join and get the items with getCollection.
      Maybe there is also a way with getCollectionGraph

      or try:

      //get category info
      $pcategories = $position->getMany('PositionCategories');
      $length = count($pcategories);
      error_log("Name: " . $this_title . " Count: " . $length);
      foreach ($pcategories as $pcategory) {
         if ($category = $pcategory->getOne('Category')){
             $this_name = $category->get('name');
             $thisCategoryNames .= $this_name . " ";
             array_push($positionClasses,$this_name);
         }
      }
      error_log("\tCategories Found :" . $thisCategoryNames);//<-- comes back empty


      to get everything within one joined query might be faster, than calling getOne many times.





        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
      • Makes sense. Thanks Bruno.