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

    I am fairly new to MODX and have been working on interfaces to talk to MODX.
    Now I got the following query:
    	[[!getCache?
    		&element=`getResources`
    		&cacheExpires=`9000`
    		&parents=`679,663,641,799,590`
    		&depth=`0`
    		&tpl=`related-article-in-page-listing`
    		&limit=`6`
    		&includeTVs=`0`
    		&includeContent=`0`
    		&showHidden=`1`
    		&tvFilters=`Related Artist==[[*Related Artist]]`
    		&toPlaceholder=`new`
    		&resources=`-[[*id]]`
    	]]
    


    that I would like to translate to a snippet so I can call it with the API via the runSnippet() method.

    I've gotten this far:
    <?php
    $output = '';
    $id = !empty($id) ? $id : 0;
    
    $modx= new modX();
    $modx->initialize('mgr');
    
    $query = $modx->newQuery('modResource');
    $query->where(array(
       'parent' => 679,
       'published' => 1,
       'Related Artists.value:=' => 603 // this will be replaced with $id when I get it to work...
    ));
    $query->limit(5);
    $titles = $modx->getCollection('modResource',$query);
    
    
    foreach ($titles as $k) {
        $output.= '<li>'.$k->get('pagetitle').'</li>'; // let's just get the titles first
    }
    
    return '<ul>'.$output.'</ul>';
    


    this will not return anything and I think I am just not clear as to what the above query actually calls in MySQL terms.
    As var as I could tell it looks for entries that have either 679,663,641,799 or 590 as parent and the TV [[*Related Artist]] looks into a flied presumably called "Related Artist" for an exact match?!

    Any help is appreciated here as I have been wrangling with the Book and online help for a couple days now wink
    Thanks
      • 3749
      • 24,544 Posts
      It would work without the Related Artists array member. TV values are in a separate table, so in order to do it with a single query (which is the right way), you need to do a left join on the modTemplateVarResource object where resourceid = the id of the resource being processed, and tmplvarid = the ID of the TV.

      The quick and dirty method (which I am guilty of using) would be to put a test like this in your foreach loop:

      $tvId = 12;  // ID of the TV
      foreach ($titles as $k) {
          if ($k->getTVValue($tvId) == 603) {
              $output.= '<li>'.$k->get('pagetitle').'</li>'; // let's just get the titles first
          }
      }
      
        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
        • 4172
        • 5,888 Posts
        if you need a TV-filter function, which genererates the filter-query here is one:

        https://forums.modx.com/thread/31946/multiple-tv-searching-via-xpdo?page=2

        Its taken mostly from getResources.
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 44320
          • 17 Posts
          Sorry for getting back to this so late

          @BobRay Thanks for the advice first of all.
          I have your book right next to me wink

          As I am working on a modx implementation that is very popular I am trying to save as much in PHP runtime as I can so I tend to go the "proper" way.

          This is what I came up with according to your comment:
          $query = $modx->newQuery('modResource');
          $query->leftJoin('modTemplateVarResource', 'tv_value'); //, array('tv_value.resourceid = resourceid')
          $query->where(array(
             'modResource.parent' => 679,
             'modResource.published' => 1,
             'tv_value.resourceid' => 679,
             'tv_value.tmplvarid' => 603 // this will be replaced with $id when I get it to work...
          ));
          $query->limit(15);
          $titles = $modx->getCollection('modResource',$query);


          Unfortunately that doesn't work. I've been up and down the forum and the rftm but am at a loss.
          The TV id is 140 and its handle is related-artist...
          Any help is appreciated!

          @Bruno17
          Thanks for the advice. I checked out your migx class and tried that too with:
          $migx = new Migx();
          $filter = 'related-artist==%603%';
          
          $query = $modx->newQuery('modResource');
          $migx->tvFilters($filter,$query);
          $query->where(array(
             'parent' => 679,
             'published' => 1
          ));
          $query->limit(15);
          $titles = $modx->getCollection('modResource',$query);
          


          That too did not result in anything. I tried munching about in the code but decided to go back to the more simple approach to not introduce more errors on my side.

          Thanks for looking into this again.
            • 4172
            • 5,888 Posts
            what do you get with

            $query->prepare();echo $query->toSql();


            try to see what you get with this generated query in phpmyadmin
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 44320
              • 17 Posts
              Hi Bruno17,

              I get the following:
              SELECT `modResource`.`id` AS `modResource_id`, `modResource`.`type` AS `modResource_type`,
              `modResource`.`contentType` AS `modResource_contentType`, `modResource`.`pagetitle` AS 
              `modResource_pagetitle`, `modResource`.`longtitle` AS `modResource_longtitle`, 
              `modResource`.`description` AS `modResource_description`, `modResource`.`alias` AS 
              `modResource_alias`, `modResource`.`link_attributes` AS `modResource_link_attributes`, 
              `modResource`.`published` AS `modResource_published`, `modResource`.`pub_date` AS 
              `modResource_pub_date`, `modResource`.`unpub_date` AS `modResource_unpub_date`, 
              `modResource`.`parent` AS `modResource_parent`, `modResource`.`isfolder` AS 
              `modResource_isfolder`, `modResource`.`introtext` AS `modResource_introtext`, 
              `modResource`.`content` AS `modResource_content`, `modResource`.`richtext` AS 
              `modResource_richtext`, `modResource`.`template` AS `modResource_template`, 
              `modResource`.`menuindex` AS `modResource_menuindex`, `modResource`.`searchable` AS 
              `modResource_searchable`, `modResource`.`cacheable` AS `modResource_cacheable`, 
              `modResource`.`createdby` AS `modResource_createdby`, `modResource`.`createdon` AS 
              `modResource_createdon`, `modResource`.`editedby` AS `modResource_editedby`, 
              `modResource`.`editedon` AS `modResource_editedon`, `modResource`.`deleted` AS 
              `modResource_deleted`, `modResource`.`deletedon` AS `modResource_deletedon`, 
              `modResource`.`deletedby` AS `modResource_deletedby`, `modResource`.`publishedon` AS 
              `modResource_publishedon`, `modResource`.`publishedby` AS `modResource_publishedby`, 
              `modResource`.`menutitle` AS `modResource_menutitle`, `modResource`.`donthit` AS 
              `modResource_donthit`, `modResource`.`privateweb` AS `modResource_privateweb`, 
              `modResource`.`privatemgr` AS `modResource_privatemgr`, `modResource`.`content_dispo` AS 
              `modResource_content_dispo`, `modResource`.`hidemenu` AS `modResource_hidemenu`, 
              `modResource`.`class_key` AS `modResource_class_key`, `modResource`.`context_key` AS 
              `modResource_context_key`, `modResource`.`content_type` AS `modResource_content_type`, 
              `modResource`.`uri` AS `modResource_uri`, `modResource`.`uri_override` AS 
              `modResource_uri_override`, `modResource`.`hide_children_in_tree` AS 
              `modResource_hide_children_in_tree`, `modResource`.`show_in_tree` AS `modResource_show_in_tree`, 
              `modResource`.`properties` AS `modResource_properties` FROM `modx_site_content` AS `modResource` 
              LEFT JOIN `modx_site_tmplvar_contentvalues` `tv_value` ON WHERE ( `modResource`.`parent` = 679 
              AND `modResource`.`published` = 1 AND `tv_value`.`resourceid` = '679' AND `tv_value`.`tmplvarid` 
              = '140' ) LIMIT 15 
              


              running it in mysql returns:
              ERROR 1064 (42000): You have an error in your SQL syntax;
              check the manual that corresponds to your MySQL server version for the right syntax to use
              near 'WHERE ( `modResource`.`parent` = 679 AND `modResource`.`published` = 1 AND `tv_v'
              at line 1
              


              ...
                • 4172
                • 5,888 Posts
                When not using the MIGX-function try this:



                $query->leftJoin('modTemplateVarResource', 'TemplateVarResources'); //schema has a defined aggregate TemplateVarResources
                $query->where(array(
                   'modResource.parent' => 679,
                   'modResource.published' => 1,
                   'TemplateVarResources.tmplvarid' => 603 // this will be replaced with $id when I get it to work...
                ));


                or

                $query->leftJoin('modTemplateVarResource', 'tv_value', 'tv_value.resourceid = resourceid'); //with a custom join-alias, you need to define a ON - statement
                $query->where(array(
                   'modResource.parent' => 679,
                   'modResource.published' => 1,
                   'tv_value.tmplvarid' => 603 // this will be replaced with $id when I get it to work...
                ));




                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 44320
                  • 17 Posts
                  Hi Bruno

                  that sort of makes sense to me but still fails. It returns nothing.
                  I am now very cautions about the values 679 and 603 that I put in there. I will triple check that this combination actually does return something.
                  In the meantime I though it might be worth mentioning that I have a constant warning:
                  Catchable fatal error: Argument 1 passed to xPDOObject::load() must be an instance of xPDO, instance of modX given in /path/core/xpdo/om/xpdoobject.class.php on line 404 


                  I figured that is not anything related but can't be sure.

                  Thanks though
                    • 44320
                    • 17 Posts
                    Ok so I checked this and ran the following query:
                    SELECT * FROM `modx_site_tmplvar_contentvalues` WHERE `tmplvarid` = '13' AND `value` LIKE '%603%'
                    

                    which returned 29 results.

                    I changed my query to:
                    $query = $modx->newQuery('modResource');
                    $query->leftJoin('modTemplateVarResource', 'tv_value', 'tv_value.resourceid = resourceid'); //with a custom join-alias, you need to define a ON - statement
                    $query->where(array(
                       'modResource.parent' => 679,
                       'modResource.published' => 1,
                       'tv_value.tmplvarid' => 13
                    ));
                    $query->limit(15);
                    $titles = $modx->getCollection('modResource',$query);
                    

                    (no returns)

                    13 is the TV ID (not 140 as previously stated by me).
                    Now I am not sure how I check for the value.

                    To my understanding the original getResource query does the following:
                    [[!getCache?
                        &element=`getResources`
                        &cacheExpires=`9000`
                        &parents=`679,663,641,799,590`
                        &depth=`0`
                        &tpl=`related-article-in-page-listing`
                        &limit=`6`
                        &includeTVs=`0`
                        &includeContent=`0`
                        &showHidden=`1`
                        &tvFilters=`Related Artist==[[*Related Artist]]`
                        &toPlaceholder=`new`
                        &resources=`-[[*id]]`
                    ]]
                    


                    In resources `679,663,641,799,590` look for pages that have the TV "Related Artists" equal to the current [[*Related Artist]].

                    so something like:
                    select content.* from [content-table] as content
                    (join with modx_site_tmplvar_contentvalues as temp)
                    where [pageID] like (679,663,641,799,590)
                    and `temp.tmplvarid`=13
                    and `temp.value`=603
                    


                    [content-table] and [pageID] is unclear to me though but whats more unclear to me is how this query would look in xPDO smiley

                    Thanks again for the help.
                    Once I get the hang of this I'll be much more productive smiley
                      • 4172
                      • 5,888 Posts
                      try:

                      $query = $modx->newQuery('modResource');
                      $query->leftJoin('modTemplateVarResource', 'tv13', 'tv13.contentid = modResource.id'); //with a custom join-alias, you need to define a ON - statement
                      $query->where(array(
                         'modResource.parent' => 679,
                         'modResource.published' => 1,
                         'tv13.tmplvarid' => 13,
                         'tv13.value' => 613
                      ));
                      $query->getSelectColumns('modResource','modResource');
                      
                      $query->limit(15);
                      $o = array();
                      if ($collection = $modx->getCollection('modResource',$query)){
                          foreach ($collection as $object){
                              $o[] = $object->get('pagetitle');
                          }
                      
                      }
                      
                      return implode('<br />',$o);
                      
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!