We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1331
    • 129 Posts
    I have an XPDO query that I'm working on and I'd like to create a left join between my custom table and the MODX table that holds the template variable content (modx_site_tmplvar_contentvalues).

    I think I've located the Schema file that has all the necessary classes for MODX in the core folder. But now I'm trying to figure out how I can connect to it and how that would be written. When I add the join to my query, the output goes blank.
      • 3749
      • 24,544 Posts
      Can you post your query?

      The object you want in your query is modTemplateVarResource.

      So maybe something like this:

      $query = $modx->newQuery('modTemplateVarResource');
      $query->leftJoin('MyClass', 'MyClass');
      
      $query->where('???');
      
      $results = $modx->getCollection('modTemplateVarResource', $query);
      

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 1331
        • 129 Posts
        Here is my mySQL query that is working properly in my tests.
        select * FROM
        	ssm_companies
        LEFT JOIN 
        	modx_site_tmplvar_contentvalues 
        ON 
        	modx_site_tmplvar_contentvalues.value LIKE ssm_companies.id
        WHERE
        	modx_site_tmplvar_contentvalues.contentid IS NOT NULL
        


        My attempted xPDO query follows.

        <?php
        $path = MODX_CORE_PATH . 'components/myPackageName/';
        $result = $modx->addPackage('myPackageName',$path .
            'model/','ssm_');
            
        $query = $modx->newQuery('Companies');
        $whereArray = array(
            'active'=>1
        );
        $query->where($whereArray);
        $query->sortby('company_name','ASC');
        $query ->leftJoin('Companies', 'SiteTmplvarContentvalues', array('Companies.id=SiteTmplvarContentvalues.value'));
        
        $companies = $modx->getCollection('Companies',$query);
        
        foreach($companies as $company) {
            $co_details = $company->toArray();
            $output .= $company->get('value');
            $output .= $modx->getChunk('tpl.company-row-pdo', $co_details);
        }
        
        return $output;
        


        This works as expected without the LEFT JOIN. But the reason I want to join these two tables is because I need to add that last part that says "WHERE modx_site_tmplvar_contentvalues.contentid IS NOT NULL"
          • 3749
          • 24,544 Posts
          I don't claim to be an expert at complex xPDO queries, but here's my best guess.

          Only the second table goes in the leftJoin (since the first one is already in the $query), and you want the object name, not the table name in the join statement. The second argument is the alias of the first, so:

          $query->leftJoin('modTemplateVarResource', 'TVR', array(
              'Companies.active => 1,
              'Companies.id' => 'TVR.value',
              /* 'TVR.contentid:!=' => null, */
          ));
          


          FYI, the TVR.contentid will *never* be null. It will always be the ID of a resource. In fact, null is not allowed (in the MySQL structure of the modx_site_tmplvar_contentvalues table) for any of the four fields of a modTemplateVarResource. Given that, I wonder if you might be overthinking this.

          Maybe I'm misunderstanding what you're doing, but it appears that you're getting all companies that have an ID that matches the value of any TV on your entire site. Can you describe what it is you're trying to collect? There might be an easier way.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 1331
            • 129 Posts
            OMG, I just wrote the longest post ever and Discuss just didn't submit it. I can't bear to write all of that again. I'll try this and report back.

            UGH!! Kidding me!
              • 3749
              • 24,544 Posts
              I hate when that happens. I try to always do Ctrl-A Ctrl-C before saving a long post, but I don't always remember.

                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 1331
                • 129 Posts
                Still working on it. Here's a quick question: Is there a way to specify the column names in the join? The two columns that I'm joining are not the same name.
                  • 1331
                  • 129 Posts
                  Nevermind, I see where that's handled. I was thrown off since the SQL in the error log is returning something unexpected in that area.
                    • 3749
                    • 24,544 Posts
                    BTW, you probably know this, but you can use this to see the actual SQL it's generating:

                    $query->prepare();
                    $sqlString = $query->toSql();


                    Also, you have the option to use PDO with a traditional query string: http://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.query
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 1331
                      • 129 Posts
                      Yeah, that's what I'm using. Still not working yet. But check out my latest progress.

                      This is a version that I tried
                      <?php
                      $path = MODX_CORE_PATH . 'components/myPackageName/';
                      $result = $modx->addPackage('myPackageName',$path .
                          'model/','ssm_');
                          
                      /*SETS COLLECTION TO GET ALL COMPANIES*/
                      $query = $modx->newQuery('Companies');
                      $query->limit(15);
                      
                      $query->leftJoin('modTemplateVarResource', 'TVR', array(
                          'Companies.id' => 'TVR.value',
                      ));
                      $query->select($modx->getSelectColumns('Companies','Companies','co_',array('id','company_name')));
                      
                      $whereArray = array(
                          'active'=>1
                      );
                      $query->where($whereArray);
                      $query->sortby('company_name','ASC');
                      
                      // prepare and dump the SQL to the log
                      $query->prepare();
                      $modx->log(xPDO::LOG_LEVEL_ERROR, $query->toSql());
                      
                      $companies = $modx->getCollection('Companies',$query);
                      
                      
                      foreach($companies as $company) {
                          $co_details = $company->toArray();
                          $output .= $modx->getChunk('tpl.company-row-pdo', $co_details);
                      }
                      return $output;
                      


                      I get all the same results as when I didn't have the left join. Notice what the SQL output says

                      SELECT `Companies`.`id` AS `Companies_id`, `Companies`.`ticker` AS `Companies_ticker`, `Companies`.`company_name` AS `Companies_company_name`, `Companies`.`company_description` AS `Companies_company_description`, `Companies`.`exchange` AS `Companies_exchange`, `Companies`.`active` AS `Companies_active` FROM `ssm_companies` AS `Companies` LEFT JOIN `modx_site_tmplvar_contentvalues` `TVR` ON `Companies`.`id` = 0 WHERE `Companies`.`active` = 1 ORDER BY company_name ASC LIMIT 15 
                      


                      You see the "ON `Companies`.`id` = 0"? Shouldn't it say "ON `Companies`.`id` = `TVR`.`value` and not a ZERO? I'm thinking it's not returning anything for TV.value because of the conflicting ID columns in the two tables. Just a guess.

                      So, I tried the same code, just with an added "getSelectColumns" in there so that I could prefix the queried columns
                      $query->select($modx->getSelectColumns('Companies','Companies','co_',array('id','company_name')));
                      

                      The prefixing worked, but didn't solve my problem. (Strangely, it caused a 404 error)

                      Furthermore, I added

                      $fields = $modx->getFields('Companies');
                      print_r($fields);


                      after my join statement and it still only returns the original set of columns.
                       Array ( [id] => [ticker] => [company_name] => [company_description] => [exchange] => [active] => 1 ) 

                      If the join was successful, would this printout show the newly combined columns?

                      I've tried so many things -including hydrating- and nothing seems to work.

                      (Part of my problem is that the documentation for xPDO doesn't include very much information beyond what the function is supposed to do. So, I'm piecing things together from different people's forum posts.)