We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I have two objects, category and subcategory.

    There is no separate db table describing the relationship - subcategory has a ’category id’ field, and in that way, categories can contain 0..infinite subcategories.

    	<object class="ResumeCategory" table="resume_category" extends="xPDOSimpleObject">
    		<field key="name" dbtype="varchar" precision="64" phptype="string" null="true" default="" />
    		<composite alias="ResumeSubcategory" class="ResumeSubcategory" local="id" foreign="category_id" cardinality="one" owner="local" />
    	</object>
    	
    	<object class="ResumeSubcategory" table="resume_subcategory" extends="xPDOSimpleObject">
    		<field key="category_id" dbtype="mediumint" precision="8" attributes="unsigned" phptype="integer" null="true" default="0" />
    		<field key="name" dbtype="varchar" precision="64" phptype="string" null="true" default="" />
    		<aggregate alias="ResumeCategory" class="ResumeCategory" local="category_id" foreign="id" cardinality="one" owner="foreign" />
    		<aggregate alias="ResumeForm" class="ResumeForm" local="id" foreign="subcat_id" cardinality="many" owner="foreign" />
    	</object>
    


    xPDO is mostly handling this relationship just fine, except for one problem. When I load a CollectionGraph of a category that does NOT contain any subcategories, I still end up with a related subcategory object, whose fields are all null.

    I have traced this, but I’m not sure where the fix/workaround should be implemented, if indeed I’m not missing something else that would solve my problem.

    The "empty" related object which represents the fact that there are no related objects is created when $xpdoquery::hydrateGraph runs, because the the query’s graph property contains an empty array (who’s key is the related object’s name).
    It seems to me that that empty array shouldn’t be there. A fix would either
    a) prevent that empty array from being created in the $xpdoquery_(db)::graph property
    b) prevent hydrateGraphNode from being called when the graph property is looped through, if the subrelations are an empty array
    c) implement the check/fix in hydrateGraph node

    Thoughts?
      Mike Schell
      Lead Developer, MODX Cloud
      Email: [email protected]
      GitHub: https://github.com/netProphET/
      Twitter: @mkschell
    • The issue was identified in this thread. It should not create the empty object instances in those cases and does need to be fixed. I’ll be entering a ticket to track this issue in the next day or two, and will post back here with details when I have done so.

      In the meantime, if you have thoughts on patching the problem, feel free to post them here.
      • The attached diff for om/xpdoquery.class.php fixes the immediate problem I was having.

        After a related object is created, then filled with values in ::fromArray(), I check to see if the primary key is null. If it is, I assume that this object should not be hydrated into related, and it is destroyed. I realize this is a huge assumption on my part, which is why I’m submitting it for review here.

        On one hand, it seems a bit late in the game to decide that this object is not meant to exist, but after examining code up and down the stack, this place made the most sense.

        I’m also not 100% confident of the method of checking the primary key. I’m checking for null values, or if it’s a compound key, that all of the fields in the key are null. Maybe empty would be a better check? Maybe the object itself should have a method to check if the primary key is empty or null... it would make the hydrateGraphNode function cleaner again.

          Mike Schell
          Lead Developer, MODX Cloud
          Email: [email protected]
          GitHub: https://github.com/netProphET/
          Twitter: @mkschell
        • Empty() will return true on a 0 value, which may cause a problem if the key is 0.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
          • Whoa - do NOT commit my above patch to xpdo.. while it solved the problem I was having in my custom app, it breaks MODx.
            I’ll get back to this issue shortly to find a better solution to my original problem.
              Mike Schell
              Lead Developer, MODX Cloud
              Email: [email protected]
              GitHub: https://github.com/netProphET/
              Twitter: @mkschell