We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20400
    • 37 Posts
    I finally decided to take a look at Revolution. Unfortunately, the I am not familiar with xPDO and working through the API’s is a bit of a struggle for me.

    What I am trying to do is get the results from a simple query "SELECT id, alias, menutitle FROM modx_site_content WHERE parent = ’0’ AND published = ’1’"

    I took a bit of a short cut and looked for some code I could modify. I copied this code from http://www.bobsguides.com/revolution-objects.html (BTW - Great site)
    $criteria = $modx->newQuery('modResource');
    $criteria->where(array(
       'parent' => $modx->resource->get('id'),
    ));
    $criteria->sortby('pagetitle','ASC');
    $children = $modx->resource->getMany('Children',$criteria);
    


    That code gets any children for the page you are on. What I wanted was any children from the root - parent=0 So I changed it to:

    $criteria = $modx->newQuery('modResource');
    $criteria->where(array(
       'parent' => 0,
    ));
    $criteria->sortby('pagetitle','ASC');
    $children = $modx->resource->getMany('Children',$criteria);
    


    But that doesn’t work.

    First, can anyone tell me why this doesn’t work? Second, what are the advantages of creating an instance of the newQuery class and running these methods over writing an SQL statement? And third, is there any way I can just write a statement - similar to the old $modx->db->query($sql)

    Thanks
      • 3749
      • 24,544 Posts
      Quote from: mregan at Nov 12, 2010, 08:42 PM

      I finally decided to take a look at Revolution. Unfortunately, the I am not familiar with xPDO and working through the API’s is a bit of a struggle for me.

      What I am trying to do is get the results from a simple query "SELECT id, alias, menutitle FROM modx_site_content WHERE parent = ’0’ AND published = ’1’"

      What I wanted was any children from the root - parent=0 So I changed it to:

      <?php
      $criteria = $modx->newQuery('modResource');
      $criteria->where(array(
         'parent' => 0,
      ));
      $criteria->sortby('pagetitle','ASC');
      $children = $modx->resource->getMany('Children',$criteria);
      


      But that doesn’t work.

      First, can anyone tell me why this doesn’t work? Second, what are the advantages of creating an instance of the newQuery class and running these methods over writing an SQL statement?

      Thanks

      It doesn’t work because you’re asking for children of the current resource that have a parent of 0. Since their parent is the current resource, you won’t get any. wink

      The getMany() and getOne methods only get objects related to a particular object that you already have a reference to (often, the current user or resource). The generic methods are getObject() and getCollection(), so I think you want:

      <?php
      $criteria = $modx->newQuery('modResource');
      $criteria->where(array(
         'parent' => '0',
         'published' => '1'
      ));
      $criteria->select(array('id','pagetitle','alias','menutitle'));
      $criteria->sortby('pagetitle','ASC');
      $children = $modx->getCollection('modResource',$criteria);
      


      Remember that you’ll get back an array of objects (or an empty array on failure) so if you want the pagetitle, you’d use something like this to display them:

      <?php
      $output = '';
      if (! empty($children)) {
          foreach($children as $child) {
             $output .= '<br />' . $child->get('pagetitle');
          }
      } else $output = 'No Children Found';
      
      return $output;
      


      OpenGeek can answer your other questions much better than I could.

        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
        • 22303 MODX Staff
        • 10,725 Posts
        Quote from: mregan at Nov 12, 2010, 08:42 PM

        Second, what are the advantages of creating an instance of the newQuery class and running these methods over writing an SQL statement?
        They are more easily portable and optimizable for other database platforms. IOW, it allows us to abstract as much as possible easily and then override only when necessary to optimize a particular query or method for a specific database engine, typically where abstraction doesn’t make sense. These queries wrapped in metadata also allow for more intelligent and flexible caching features that can greatly reduce database loads in high-traffic situations. I also think the code is easier to read, and in general, doesn’t require those reading the code to know SQL.

        Quote from: mregan at Nov 12, 2010, 08:42 PM

        And third, is there any way I can just write a statement - similar to the old $modx->db->query($sql)
        Yep, just use any of the PDO methods which are available directly from the $modx object, e.g.
        <?php
        $stmt = $modx->prepare("SELECT * FROM `food` WHERE `type` = ?");
        if ($stmt && $stmt->execute(array('beer'))) {
            var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
        }
        
        $stmt = $modx->query("SELECT `name` FROM `food` WHERE `type` = 'beer'");
        if ($stmt) var_dump($stmt->fetchAll(PDO::FETCH_COLUMN));
        
        $affectedRows = $modx->exec("UPDATE `food` SET `rating` = 'EXCELLENT' WHERE `type` = 'beer'");
        

        You can also take advantage of result set caching in xPDO by using xPDOCriteria directly (which xPDOQuery is a more advanced derivative form of), e.g.
        <?php
        $criteria = new xPDOCriteria($modx, "SELECT * FROM `food` WHERE `type` = :type", array('type' => 'beer'));
        $stmt = $criteria->prepare();
        if ($stmt && $stmt->execute()) {
            var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
        }
        

        Note you can also use raw SQL within the xPDOQuery criteria as well, assuming a custom class called myFood in an opengeek package, e.g.
        <?php
        $beers = $modx->getCollection('myFood', array(
            array('rating' => 'EXCELLENT'),
            "`type` LIKE '%beer%' OR `type` IN ('wine','spirits')"
        ));
        
          • 20400
          • 37 Posts
          @OpenGeek, @BobRay

          Thanks - That explains a lot. laugh

          I found
          $modx->getChildIds(0, 1);
          will give me the IDs - then I can walk through the array and get each object. But that generates unnecessary calls to the database.

          However, setting up the query with ’prepare’ worked like a charm with only one db call.
            • 18985
            • 29 Posts
            Is there a reason why $stmt->execute() could fail. I can't seem to get it to work