We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14638
    • 14 Posts
    Hi,
    I’m having this issue where I’m expecting a certain result and getting something else. I’m try to filter by the following logic

    (cond1 AND cond 2) OR (cond3 AND cond2) using the following getResources call...

    [[!getResources? &parents=`0` &limit=`100` &tpl=`channel-feed-item` &includeTVs=`1` &includeContent=`1` &tvFilters=`channels==music,content-types==News||cross-channel==music,content-types==News`]]


    What it seems to be returning is:
    (cond1 AND cond 2) OR cond3 OR cond2

    Has anyone had any experience doing this type of getResources call?

    Thanks in advance.
      • 14915
      • 43 Posts
      Hi,

      I was looking at this with Jon yesterday. I’m not entirely sure, but the problem may lie somewhere here:

      if (!empty($conditions)) {
              foreach ($conditions as $cGroup => $c) {
                  if ($cGroup > 0) {
                      $criteria->orCondition($c, null, $cGroup);
                  } else {
                      $criteria->andCondition($c);
                  }
              }
          }
      


      or, if not there, then in the section above it:

      if (!empty($tvFilters)) {
          $tmplVarTbl = $modx->getTableName('modTemplateVar');
          $tmplVarResourceTbl = $modx->getTableName('modTemplateVarResource');
          $conditions = array();
          foreach ($tvFilters as $fGroup => $tvFilter) {
              $filterGroup = count($tvFilters) > 1 ? $fGroup + 1 : 0;
              $filters = explode(',', $tvFilter);
              foreach ($filters as $filter) {
                  $f = explode('==', $filter);
                  if (count($f) == 2) {
                      $tvName = $modx->quote($f[0]);
                      $tvValue = $modx->quote($f[1]);
                      $conditions[$filterGroup][] = "EXISTS (SELECT 1 FROM {$tmplVarResourceTbl} `tvr` JOIN {$tmplVarTbl} `tv` ON `tvr`.`value` LIKE {$tvValue} AND `tv`.`name` = {$tvName} AND `tv`.`id` = `tvr`.`tmplvarid` WHERE `tvr`.`contentid` = `modResource`.`id`)";
                  } elseif (count($f) == 1) {
                      $tvValue = $modx->quote($f[0]);
                      $conditions[$filterGroup][] = "EXISTS (SELECT 1 FROM {$tmplVarResourceTbl} `tvr` JOIN {$tmplVarTbl} `tv` ON `tvr`.`value` LIKE {$tvValue} AND `tv`.`id` = `tvr`.`tmplvarid` WHERE `tvr`.`contentid` = `modResource`.`id`)";
                  }
              }
          }
          if (!empty($conditions)) {
              foreach ($conditions as $cGroup => $c) {
                  if ($cGroup > 0) {
                      $criteria->orCondition($c, null, $cGroup);
                  } else {
                      $criteria->andCondition($c);
                  }
              }
          }
      }
      
      
        • 8898
        • 181 Posts
        I changed the line
                $filterGroup = count($tvFilters) > 1 ? $fGroup + 1 : 0;

        (line 162) into
                $filterGroup = count($tvFilters) > 1 ? $fGroup : 0;


        In my case this works. If this isn’t changed, in the block
                foreach ($conditions as $cGroup => $c) {
                    if ($cGroup > 0) {
                        $criteria->orCondition($c, null, $cGroup);
                    } else {
                        $criteria->andCondition($c);
                    }
                }

        (starting at line 177) an andCondition is never added when there are more than two conditions delimited by || in the tvFilters Property, and this should lead to a wrong result, if I understand the whole construct correctly. Alternatively, the line
                    if ($cGroup > 0) {

        could be changed to
                    if ($cGroup > 1) {


        I also posted this in the issue tracker: https://github.com/opengeek/getResources/issues#issue/9.

        Cheers,
        Jan
          This message has been ROT-13 encrypted twice for higher security.
          • 33968
          • 863 Posts
          As far as I know, this hasn’t been resolved with the latest getResources (1.3.0-pl).

          I made the change suggested (line 162 only) and it’s now working for me. Haven’t tested thoroughly though smiley

          Cheers guys

          *EDIT*
          Have tested a bit more and with the above fix, getResources seems to ignore the &parents property and displays resources from ALL parents. This happens when there are multiple filters present.