We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27738
    • 2 Posts
    Hi Peeps,

    I’m just starting out with modx revo and xpdo. I’m reasonably sorted with modx and building a site but I’ve fallen at the first hurdle with accessing my custom tables. I searched around and found http://bobsguides.com/custom-db-tables.html and managed to create the xml scheema and class files and was able to successfully access a single table and retrieve data but fell over trying to join tables. I could retrieve data from the parent table but could not get the data from the child table. I checked my tables with SQL and all worked. I then tried http://rtfm.modx.com/display/xPDO20/More+Examples+of+xPDO+XML+Schema+Files.

    I used the tables defns to create the tables, reran bobs snippet to generate the schema and class code then pasted the new xml schema code into my schema and put snippet code on a resource. When I viewed the page it retrieved the data from the primary table but not the child table. If I run the code on the child table as a non join query i can retrieve data - just cann’t get the data from the join tables whn I run the join code.

    What am I doing wrong - any advice gratefully received before I loose what little hair I have got left :’(.
    This is the snippet code

    $path = MODX_CORE_PATH . 'components/soshul/';
    $result =$modx->addPackage('soshul',$path . 'model/','shl_');
    $output = '';   
    
    if (! $result) {
      return 'failed to add package';
    } else {
      $output = '<p>added Package</p>';
    }
    $output .='<p>below is the join results.</p>';
    $blogpost = $modx->getObject('Blogposts', array('blogpost_id' => 1 ) ); 
    $comments = $blogpost->getMany('Comments');   
    $output .= $blogpost->get('content');   
    foreach ( $comments as $c ) 
    {
         $output .= $c->get('comment'); 
    }
    //now test to see if i can retrieve from the comment table as a single table
    $output .='<br /><br />';
    $output .='<p>Below is the data from the child table so I can access it as a single table query:</p>';
    $coms = $modx->getObject('Comments', array('comment_id' => 1 ) ); 
    $output .='<p>';
    $output .= $coms->get('comment');
    $output .='</p>';
    
    
       return $output;
    


    This is the out put on the resource when viewed in browser
    added Package
    
    below is the join results.
    This is blog post 1
    
    Below is the data from the child table so I can access it as a single table query:
    
    1 comment 1
    

    below is the schema code
    <object class="Blogposts" table="blogposts" extends="xPDOObject">
    	<field key="blogpost_id" dbtype="int" precision="11" phptype="integer" null="false" index="pk"  generated="native" />
    	<field key="content" dbtype="text" phptype="string" null="true" />
    	<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
    		<column key="blogpost_id" collation="A" null="false" />
    	</index>
    	<composite alias="Comments" class="Comments" local="blogpost_id" foreign="blogpost_id" cardinality="many" owner="local" />
    </object>
    
    <object class="Comments" table="comments" extends="xPDOObject">
    	<field key="comment_id" dbtype="int" precision="11" phptype="integer" null="false" index="pk"  generated="native" />
    	<field key="blogpost_id" dbtype="int" precision="11" phptype="integer" null="true" />
    	<field key="comment" dbtype="text" phptype="string" null="true" />
    	<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
    		<column key="comment_id" collation="A" null="false" />
    	</index>
    	<aggregate alias="Blogposts" class="Blogposts" local="blogpost_id" foreign="blogpost_id" cardinality="one" owner="foreign" />
    </object>
    

      • 18373 ☆ A M B ☆
      • 3,141 Posts
      Looks fine to me huh

      You say you copied back the schema from what was generated - are you also doing the same with the models? You should use the same "revision" of model/schema.. that’s the only thing I can think of right now...
        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.
        • 27738
        • 2 Posts
        Hi Mark,

        Thanks for the reply - nice to know I’m not going mad!!!! shocked

        I think what I shall do as soon as I get the opportunity is to clear the lot and start from fresh then regenerate the schema without copying and pasting and see what that gives me. There must be something in there thats throwing a spanner..............

        Thanks again smiley
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          Having developed a couple of CMPs myself I prefer to just write the schema myself, generate the model from it and having a script generate the related tables. It’s a bit more complicated and maybe takes more time as well than reverse engineering like you are doing, but it gives more control.

          Something to keep in mind for a different time perhaps tongue
            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.
            • 27738
            • 2 Posts
            Thanks for the advice, I know it is the right way to go - If only sleep didn’t get in the way.....................
              • 27738
              • 2 Posts
              HANGING MY HEAD IN SHAME :’(

              Sorted!!!!!

              As clever as I like to think I am – I never cease to amaze myself at just how bloody stupid I can be sometimes!?!?!?!?!!?!?!

              I couldn’t possibly admit to the fact that I wasn’t overwriting the map and class files.