We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38415 ☆ A M B ☆
    • 70 Posts
    I know the title sounds wired... What I'm trying to archive is:

    Get all values (from all resources) of my tag TV (lets call it "myTagTV"), but only from resources where my second TV (lets call it "productType") has a specific value.

    Example:
    Resource 1
    myTagTV: one,two,three
    productType: single
    
    Resource 2
    myTagTV: three,four,five
    productType: single
    
    Resource 3
    myTagTV: four,five,six
    productType: double


    Now I want all "myTagTV" values where "productType" is "single". So the expected result for the example above would be: one,two,three,four,five. (without six)

    Can anybody help me with code?

    This is what I have (gives me all TV values, without the filtering):
    $c = $modx->newQuery('modTemplateVarResource');
        $c->innerJoin('modTemplateVar','TemplateVar');
        $c->innerJoin('modResource','Resource');
        $c->where(array(
            'TemplateVar.name' => 'myTagTV'
            'Resource.published' => '1'
        ));
        $resourceTags = $modx->getCollection('modTemplateVarResource',$c);
      chsmedien - Digital Marketing Agency
      MODX Professional & Ambassador

      http://chsmedien.com | http://twitter.com/christianseel
      • 29661
      • 116 Posts
      Hello,

      I would try to split the requested to two queries, first will return IDs of resources, where your productType TV has a specific value (single, in this case). Second query will return values of TV, for those resources (from the first query).

      $c = $modx->newQuery('modTemplateVarResource');
      $c->innerJoin('modTemplateVar','TemplateVar');
      $c->innerJoin('modResource','Resource');
      $c->where(array(
          'TemplateVar.name' => 'productType',
          'modTemplateVarResource.value' => 'single',
          'Resource.published' => '1'
      ));
      $c->select($modx->getSelectColumns('modTemplateVarResource', 'modTemplateVarResource', '', array('contentid')));
      
      $c->prepare();
      $c->stmt->execute();
      $res = $c->stmt->fetchAll(PDO::FETCH_COLUMN, 0);
      
      $c = $modx->newQuery('modTemplateVarResource');
      $c->innerJoin('modTemplateVar','TemplateVar');
      $c->innerJoin('modResource','Resource');
      $c->where(array(
          'TemplateVar.name' => 'myTagTV',
          'Resource.published' => '1',
          'modTemplateVarResource.contentid:IN' => $res
      ));
      
      $resourceTags = $modx->getCollection('modTemplateVarResource',$c);


      It's theoretical smiley give it a shot
        --
        John
        @theboxer
        • 4172
        • 5,888 Posts
        not sure, if this is completely correct, but you can try it:

        $typeTVid = '5';
        $tagTVid = '7';
        $type = 'single';
        
        $c = $modx->newQuery('modResource');
        $c->select($modx->getSelectColumns('modResource','modResource','',array('id')));
        $c->select('tag.value');
        $c->leftJoin('modTemplateVarResource','type','type.tmplvarid = ' . $typeTVid . ' AND type.contentid = modResource.id');
        $c->leftJoin('modTemplateVarResource','tag','tag.tmplvarid = ' . $tagTVid . ' AND tag.contentid = modResource.id');
            $c->where(array(
                'modResource.published' => '1',
                'type.value' => $type
            ));
        


        may be you would be better and much faster to store all tags OnDocFormSave into a separate table.
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 3749
          • 24,544 Posts
          Both interesting approaches, but I'm not sure I see the need for the joins since it looks like all the information you need is in the table holding the modTemplateVarResource objects, no?
            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
            • 29661
            • 116 Posts
            You are right, one join is unnecessary there, but @christianseel want to take TVs only from published resources, so they have to be there.
              --
              John
              @theboxer
              • 38415 ☆ A M B ☆
              • 70 Posts
              Oh interesting ideas! Thanks so far - will give it a try tomorrow.

              For the performance: I have to do this for 3 different tag TVs. So it might be better to use the split idea and do the filtering once and select the other values in 3 independent queries?

              Or would it be possible to combine 3 tag TVs?
                chsmedien - Digital Marketing Agency
                MODX Professional & Ambassador

                http://chsmedien.com | http://twitter.com/christianseel
                • 38415 ☆ A M B ☆
                • 70 Posts
                Thanks a lot guys! I managed to get my expected result. I used the split example since I'm going to need the resource IDs later again so it makes sense to have them in an additional query. Also I removed the joins for the 2nd query.
                  chsmedien - Digital Marketing Agency
                  MODX Professional & Ambassador

                  http://chsmedien.com | http://twitter.com/christianseel