We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 813 ☆ A M B ☆
    • 326 Posts
    I’ve read the documentation, but I’m still having trouble figuring out how to use related objects in Revo. Not sure if it’s a schema problem or just how I’m using it once I’ve retrieved it. Can anyone help? I’m still learning Revo/xPDO, so please bear with me

    In this case, I have a third-party table with lots of rows, and each row contains a field called userid, which is supposed to correspond with the id for a user in the modUser class. All I want to be able to do is iterate through each row of my third-party table and display user-associated fields (such as fullname, or possibly some extended fields) with each row.

    The schema for my third-party DB object looks like this:

    <object class="CakeStats" table="cake_stats" extends="xPDOSimpleObject">
    		<field key="userid" dbtype="int" precision="11" phptype="integer" null="true" />
    		<field key="year" dbtype="varchar" precision="11" phptype="string" null="true" />
    		<field key="position" dbtype="varchar" precision="11" phptype="string" null="true" />
    		<field key="week" dbtype="varchar" precision="11" phptype="string" null="true" />
    		<field key="statName" dbtype="varchar" precision="40" phptype="string" null="true" />
    		<field key="value" dbtype="int" precision="11" phptype="integer" null="true" />
    		<aggregate alias="AggUser" class="modUser" local="userid" foreign="id" cardinality="one" owner="foreign" />
    	</object>


    You’ll notice the last line, which I thought was supposed to hook me up with the modUser object.

    From there, I’m trying to iterate through it like this:


    $path = MODX_CORE_PATH . 'components/stats/';
    $result = $modx->addPackage('stats',$path . 'model/','modx_');
    $state_abbr = $modx->getOption('state_abbr');
    $stats = $modx->getCollection('CakeStats'.$state_abbr);
    
    
    foreach($stats as $stat) {
        $userid = $stat->get('userid');
        $username = $stat->getOne('username');
    
    	       echo $userid;
    	       echo $username;
    }


    This returns the userid value for each, but not the username. Can anyone tell me what I’m doing wrong? And while you’re looking, what will I need to do differently to access the extended modUser fields in this situation?
      • 28215
      • 4,149 Posts
      getOne returns the related *object* - not a field. So it returns the modUser, according to your schema.

      /** @var modUser $user */
      $user = $stat->getOne('AggUser');
      if ($user) {
        $user->get('username');
      }
      
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 813 ☆ A M B ☆
        • 326 Posts
        Thanks for the reply, and that does help me understand what it’s returning. However, it still isn’t showing any values. Here’s the entirety of my code:

        $path = MODX_CORE_PATH . 'components/stats/';
        $result = $modx->addPackage('stats',$path . 'model/','modx_');
        $state_abbr = $modx->getOption('state_abbr');
        $stats = $modx->getCollection('CakeStats'.$state_abbr);
        
        foreach($stats as $stat) {
            $userid = $stat->get('userid');
            $user = $stat->getOne('AggUser');
            if ($user) {
                $username = $user->get('username');
            }
            echo $userid;
            echo $username;
        }


        Anything jumping out at you?
          • 813 ☆ A M B ☆
          • 326 Posts
          Oh...by the way, I changed my object’s class name in the schema for clarity for this discussion, but it may have made it more confusing. This is a multi-site, one for each state, so I have a class for each state. That’s why there’s the weird $state_abbr variable in my getCollection call, but not in my schema.
            • 28215
            • 4,149 Posts
            I’m guessing it’s because a typo:

            $modx->getCollection('CakeStats'.$state_abbr);

            should be:

            $modx->getCollection('CakeStats',array('statName' => $state_abbr));


            I assume that your table alias is CakeStats; and that $state_abbr is the name of the row you’re grabbing?

            If not, you definitely shouldn’t be adding the abbr to the end of the class name...
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 813 ☆ A M B ☆
              • 326 Posts
              No, not a typo; I am adding the abbreviation to the end of the class name, because there is a class for each state (i.e., CakeStatsTX for Texas, CakeStatsFL for Florida, CakeStatsCA for California, etc.). When you say not to do this, did you mean because it wouldn’t return anything if this wasn’t actually in the class name, or because this is bad practice? Because it works. I can retrieve the collection without a problem. I just can’t seem to get the aggregate object that goes with it.
                • 813 ☆ A M B ☆
                • 326 Posts
                Just thought of something else that it could be. I created my schema using formit2db, but I added the aggregate line to my schema file manually. Any chance this needs to be added elsewhere in the model, other than just in my schema?
                  • 4172
                  • 5,888 Posts
                  after changing something on your xml-schema you need to recreate your map-files by running
                  this part again:
                  $generator->parseSchema($schemafile, $modelpath);
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 813 ☆ A M B ☆
                    • 326 Posts
                    In retrospect, I should’ve figured out that I needed to rerun this. @Bruno, shouldn’t this automatically be added whenever the snippet is re-run? I had to modify the snippet a bit to meet my DB storage needs, but the schema generation part is still intact. Is there a way to tell if it’s been added?

                    Sorry for the silly questions. xPDO seems awesome and is growing on me very quickly, but some of it is still confusing me quite a bit.
                      • 4172
                      • 5,888 Posts
                      just modified db2formit to allow recreating the map files with &parseSchema=`1` in the Formit-snippet-call.
                      Don’t forget to remove this property when done. Otherwise the snippet will recreate the map-files all time it’s running.
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!