We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44195
    • 293 Posts
    Hi, working on a new package and thought perhaps someone with less tired eyes could spot the problem wink
    I've got a one-to-one relationship between two tables and I'm attempting to do a left join to display values from both in a CMP grid.

    The error message I'm getting is:
    [2016-12-29 12:46:58] (ERROR @ /var/www/public/modx-te/public_html/core/xpdo/om/xpdoobject.class.php : 240) Error 42S22 executing statement: 
    Array
    (
        [0] => 42S22
        [1] => 1054
        [2] => Unknown column 'Country.id' in 'field list'
    )
    


    The relevant parts of the schema are:
        <object class="LocationsCountry" table="locations_country" extends="xPDOSimpleObject">
            <field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
            <field key="position" dbtype="int" precision="10" phptype="integer" null="false"/>
            <composite alias="CountryProfile" class="LocationsCountryProfile" local="id" foreign="internalKey" cardinality="one" owner="local" />
        </object>
    
        <object class="LocationsCountryProfile" table="locations_country_profile" extends="xPDOSimpleObject">
            <field key="internalKey" dbtype="int" precision="10" phptype="integer" null="false" index="unique" />
            <field key="description" dbtype="text" phptype="text" null="false" default="" />
            <index alias="internalKey" name="internalKey" primary="false" unique="true" type="BTREE">
                <column key="internalKey" length="" collation="A" null="false" />
            </index>
            <aggregate alias="Country" class="LocationsCountry" local="internalKey" foreign="id" cardinality="one" owner="foreign" />
        </object>
    

    (Just the bare-minimum amount of fields for now until I get it working.)

    And my GetList Processor:
    <?php
    /**
     * Get a list of Countries
     *
     * @package locations
     * @subpackage processors
     */
    class LocationsCountryGetListProcessor extends modObjectGetListProcessor {
        public $classKey = 'LocationsCountry';
        public $languageTopics = array('locations:default');
        public $defaultSortField = 'position';
        public $defaultSortDirection = 'ASC';
        public $objectType = 'locations.country';
    
        public function prepareQueryBeforeCount(xPDOQuery $c) {
            $c->leftJoin('LocationsCountryProfile','CountryProfile');
    
            $query = $this->getProperty('query');
            if (!empty($query)) {
                $c->where(array(
                        'Country.name:LIKE' => '%'.$query.'%'
                    ));
            }
            return $c;
        }
    
        public function prepareQueryAfterCount(xPDOQuery $c) {
            $c->select($this->modx->getSelectColumns('LocationsCountry','Country'));
            $c->select($this->modx->getSelectColumns('LocationsCountryProfile','CountryProfile'));
            return $c;
        }
    
    }
    return 'LocationsCountryGetListProcessor';
    

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

      I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
      Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
    • Try getting rid of line 28 (selecting the LocationsCountry as Country) and changing line 21 to either just refer to name or LocationsCountry.name. I'd also put the select call into the BeforeCount instead of After Count.
        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.
        • 44195
        • 293 Posts
        Thanks! That definitely got rid of the error message, however now it's returning only the values from the LocationsCountryProfile object.

        Here's the updated processor:
        <?php
        /**
         * Get a list of Countries
         *
         * @package locations
         * @subpackage processors
         */
        class LocationsCountryGetListProcessor extends modObjectGetListProcessor {
            public $classKey = 'LocationsCountry';
            public $languageTopics = array('locations:default');
            public $defaultSortField = 'position';
            public $defaultSortDirection = 'ASC';
            public $objectType = 'locations.country';
        
            public function prepareQueryBeforeCount(xPDOQuery $c) {
                $c->leftJoin('LocationsCountryProfile','CountryProfile');
                $c->select($this->modx->getSelectColumns('LocationsCountryProfile','CountryProfile'));
                $query = $this->getProperty('query');
                if (!empty($query)) {
                    $c->where(array(
                            'name:LIKE' => '%'.$query.'%'
                        ));
                }
                return $c;
            }
        
        }
        return 'LocationsCountryGetListProcessor';
        
          I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
          Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
        • discuss.answer
          • 44195
          • 293 Posts
          Got it working although not entirely sure I understand why.

          The working answer is:
          <?php
          /**
           * Get a list of Countries
           *
           * @package locations
           * @subpackage processors
           */
          class LocationsCountryGetListProcessor extends modObjectGetListProcessor {
              public $classKey = 'LocationsCountry';
              public $languageTopics = array('locations:default');
              public $defaultSortField = 'position';
              public $objectType = 'locations.country';
          
              public function prepareQueryBeforeCount(xPDOQuery $c) {
                  $c->leftJoin('LocationsCountryProfile','CountryProfile');
                  $c->select('LocationsCountry.id,name,position');
                  $c->select($this->modx->getSelectColumns('LocationsCountryProfile','CountryProfile','',array('description')));
                  $query = $this->getProperty('query');
                  if (!empty($query)) {
                      $c->where(array(
                              'LocationsCountry.name:LIKE' => '%'.$query.'%'
                          ));
                  }
                  return $c;
              }
          }
          return 'LocationsCountryGetListProcessor';
          


          However, if I swap out line 16 for:
          $c->select($this->modx->getSelectColumns('LocationsCountry','Country','',array('id','name','position')));
          

          ... it doesn't work. Can anyone explain why?
            I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
            Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
          • fix this line:
            $c->leftJoin('LocationsCountryProfile','CountryProfile', 'CountryProfile.country_id = LocationsCountry.id');
              Rico
              Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
              MODx is great, but knowing how to use it well makes it perfect!

              www.virtudraft.com

              Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

              Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

              Maintainter/contributor of Babel

              Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.