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

    I need the ID of a document based on the value of a tv. I’m writing a snippet so it’s not a front end ditto type situation. The value is an email address and my logic doesn’t allow duplicates so I ’should’ only ever get one record returned. I’m imagining that the DB API is the way to go but I wondered if anyone knew of anything less raw (as in, through the standard API) that I could look at.

    Thank you for reading wink
      Hmmmmm.

      It's all a delusion
      • 10449
      • 956 Posts
      There’s probably several ways to do this, but I would just use Ditto with a filter clause:

      <?php
      // snippet TV2ID
      // example usage: [[TV2ID? &find=`[email protected]` &tv=`userEmail`]]
      
      
      $output = '';
      
      
      if($find && $tv) {
      
      	// run snippet:
      	$p['parents'] = 15;
      	$p['tpl'] = '@CODE [+id+]'; // depends what you want to return: this just gives you the document id
      	$p['total'] = 1;
      	$p['depth'] = 5;
      	$p['display'] = "all";
      	$p['filter'] = "$tv,$find,1"; // see http://ditto.modxcms.com/files/snippet-ditto-php.html#filter for options
      	$p['showPublishedOnly'] = 1;
      	$p['showInMenuOnly'] = 1;
      	$output = $modx->runSnippet('Ditto', $p);
      
      	return($output);
      
      } else {
      	return false;
      }
      ?>
      



      I’m writing a snippet so it’s not a front end ditto type situation.
      $modx->runSnippet() is your friend smiley
        • 1634
        • 51 Posts
        Hey Ganesh,

        Cheers wink

        I had thought of runsnippet but am concerned that its a JCB to smash a proverbial nut. Having said that I often end up calling ditto many many times on a page, another call (in fact this is at one end of an AJAX call so will be the ONLY ditto call) wont make a whole bunch of difference.

        Cheers for the snippet wink

        PS:

        Are you from the indian sub-continent?? I’m going to Kerela for a month in a couple of weeks.
          Hmmmmm.

          It&#39;s all a delusion
          • 10449
          • 956 Posts
          Well, I’m not aware of any API function like $modx->selectDocumentByTvValue(), that would of course be ideal here.
          The alternative would be to use 2-3 mySQL queries to get the same information.

          If you’re concerned about performance, add this at the end of your modx-template/document:
          MySQL: [^qt^], [^q^] request(s), PHP: [^p^], total: [^t^], document retrieved from [^s^].

          It’s a good way to spot bottlenecks.


          Are you from the indian sub-continent?? I’m going to Kerela for a month in a couple of weeks.
          Nope, in fact I’m from Switzerland. But I’ve been to India a couple of times. It just so happens that Ganesh is my favorite Hindu God smiley
          Enjoy your trip to Kerala. It’s beautiful.