We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29796
    • 91 Posts
    i have this code in evo to display some resources list ... i want to transfer it to revo but i read that revo uses PDO for queryies ... can someone explain me how to do this example with PDO ?

    <?
    if($id) $parent = $id; else $parent = $modx->documentIdentifier;
    $query = $modx->db->query( "SELECT id,pagetitle,introtext FROM site_content WHERE parent=$id AND published=1 AND deleted=0 AND isfolder=0 ORDER BY menuindex");
    $results = $modx->db->makeArray( $query );
     
    echo '<ul>';
    foreach($results as $r){
         echo '<li>';
              echo '<a href="[~'.$r['id'].'~]">'.$r['pagetitle'].'</a>';
              echo '<p>'.$r['introtext'].'</p>';
         echo '</li>';
    }
    echo '<ul>';
    ?>


    i know that there are other allready made solutions to get resources... but my question is how to do a simple custom query from modx sql tables
      • 28215
      • 4,149 Posts
      What would be best is for you to use the chunk ability in modx. This snippet (let’s call it "GetChildren") does the same as yours:

      $id = $modx->getOption('id',$scriptProperties,$modx->resource->get('id'));
      $tpl = $modx->getOption('tpl',$scriptProperties,'');
      $wrapperTpl = $modx->getOption('wrapperTpl',$scriptProperties,'');
      $sortBy = $modx->getOption('sortBy',$scriptProperties,'menuindex');
      $sortDir = $modx->getOption('sortDir',$scriptProperties,'ASC');
      
      $c = $modx->newQuery('modResource');
      $c->where(array(
          'parent' => $id,
          'published' => true,
          'deleted' => false,
          'isfolder' => false,
      ));
      $c->sortby($sortBy,$sortDir);
      $resources = $modx->getCollection('modResource',$c);
      
      $output = '';
      foreach ($resources as $resource) {
          $resourceArray = $resource->toArray();
          $output .= !empty($tpl) ? $modx->getChunk($tpl,$resourceArray) : print_r($resourceArray,true);
      }
      
      if (empty($wrapperTpl)) return $output;
      return $modx->getChunk($wrapperTpl,array(
          'resources' => $output,
      ));
      


      Then create a Chunk you called "gcWrapperTpl" that contains the wrapper:

      <ul>[[+resources]]</ul>
      


      Then create a Chunk called "gcTpl" that is for each resource result:
      <li>
        <a href="[[~[[+id]]]]">[[+pagetitle]]</a>
        <p>[[+introtext]]</p>
      </li>
      


      And finally, call it here:

      [[!GetChildren? &id=`IDGOESHERE` &tpl=`gcTpl` &wrapperTpl=`gcWrapperTpl`]]
      



      Obviously you could extend and improve the snippet to use more options....but then you could always use getResources, which does pretty much the same thing:

      <ul>
      [[!getResources? &tpl=`gcTpl` &hideContainers=`1` &depth=`1` &parents=`IDGOESHERE` &sortby=`menuindex`]]
      </ul>
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 29796
        • 91 Posts
        hey thanx for the answer ... it helped me a lot ... now i just have to figure out the way how to work from now on .... i don’t like to have one snippet and two chunks for a simple thing like this. Yes i know that keep the logic away from html code is good, and i use this technique for big projects, but for smaller one is a simple custom query end echo in a snippet the fastest way to finish it.

        I tryed drupal in the past .. and i hate it because i need it three or four templates for a simple thing ... the same problems i had working with magento commerce ... i hope that modx revo is not at the same path.

        PDO is good thing, but in my case i work 99% on mysql DB, so i don’t like that modx revo will get rid of $modx-> class for custom queryies.


        thanx again...