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

    I have a resource with some childresources.
    Now I want to grab all TV-Values from the tmplvarid 18, where the contentid is a child…

    Hope you know what I mean.
    My code shows what I mean…
    $currentID = $modx->resource->get('id');	
    $childIDs = $modx->getChildIds($currentID,1,array('context' => 'web'));
    $childIDsString = implode(',', $childIDs);
    $sql= "SELECT value FROM `modx_site_tmplvar_contentvalues` WHERE contentid IN ($childIDsString) AND tmplvarid = '18'";
    mysql_query($sql);


    The problem I have, is that I don't know how I can this make for modx and xpdo
    The documentation of xpdo isn't really nice I found, so I hope, that you can give me the correct syntax for my problem…

    And after that I hope, that I understand these a little bit more…

    Hope for your help. <3

    Kind regards,
    z.
      • 34012
      • 88 Posts
      Hello zapzarap,

      $criteria = $modx->newQuery('modTemplateVarResource');
      $criteria->select(array($modx->getSelectColumns('modTemplateVarResource', 'modTemplateVarResource', '', array('id', 'value')))); //Primary key has to be selected
      $criteria->where(array('contentid:IN' => $childIDs, //IN accepts array
          'tmplvarid' => 18
      ));
      $results = $modx->getCollection('modTemplateVarResource', $criteria);


      Pretty much like that smiley

      The getSelectColumns is used to escape fields properly, this enabling that the snippet could be ported to another DB platform.
        Almost retired from web-development industry but still randomly writing at Lazylegs.info and on schedule hopefully in near future to finish Oracle and PostgreSQL ports of MODX
        • 38537
        • 34 Posts
        Hey lazylegs thanks so far,

        I used your code, but nothing happend.
        Wenn I user
        return $results;
        I became "Array".
        And when I try:
        	foreach($results as $data){
        		echo $data."<br>";
        	}

        Then I became an error message…

        The documentation of getCollection say's nothing about the output.. =/
          • 34012
          • 88 Posts
          The $data is a object, you can access the values by using get().

          Few examples:

          foreach($results as $data) 
          {
              $out[] = $data->value;
              // OR
              $out[] = $data->get('value');
          }
          return implode("\n", $out);
          


          You should not echo anything from the snippets, always return the data. The $results is collection of xPDOObjects thats why it shows array for you.
            Almost retired from web-development industry but still randomly writing at Lazylegs.info and on schedule hopefully in near future to finish Oracle and PostgreSQL ports of MODX
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: zapzarap at Aug 10, 2012, 08:15 AM
            I have a resource with some childresources.
            Now I want to grab all TV-Values from the tmplvarid 18, where the contentid is a child…

            $currentID = $modx->resource->get('id');	
            $childIDs = $modx->getChildIds($currentID,1,array('context' => 'web'));
            $childIDsString = implode(',', $childIDs);
            $sql= "SELECT value FROM `modx_site_tmplvar_contentvalues` WHERE contentid IN ($childIDsString) AND tmplvarid = '18'";
            mysql_query($sql);


            Here is an example of how to do this with the API...
            $currentID = $modx->resource->get('id');
            $childIDs = $modx->getChildIds($currentID,1,array('context' => 'web'));
            
            /* build the query */
            $query = $modx->newQuery('modTemplateVarResource', array('tmplvarid' => 18, 'contentid:IN' => $childIDs));
            /* select just the value column */
            $query->select($modx->getSelectColumns('modTemplateVarResource', '', '', array('value')));
            $values = array();
            /* prepare and execute a PDOStatement from the $query */
            if ($query->prepare() && $query->stmt->execute()) {
                /* use PDOStatement::fetchColumn() to get an array representing 
                    the first column of the result set */
                $values = $query->stmt->fetchColumn();
            }
            
              • 38537
              • 34 Posts
              Thanks so much lazylegs.
              Works fine for me, but I have an other problem yet.

              All these child-resources hav 2 TVs.
              Now I want to became all child-resources, where the first AND the second TV has exact this value, for what I search.

              I tried something, but that doesn't work for me…

              Would be realy nice if you help me again. And thats definitely my last problem ((:

              kind regards,
              z.