We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4172
    • 5,888 Posts
    I see, yes this works, when I change to $c->limit(10,0);

    Thanks.
      -------------------------------

      you can buy me a beer, if you like MIGX

      http://webcmsolutions.de/migx.html

      Thanks!
      • 28215
      • 4,149 Posts
      Yes, my bad. $limit then $start.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
      • I may have found a workaround for my issue.
        Please correct me if i’m wrong:

        <?php
        $c = $this->modx->newQuery('Model');
        $c->select('id, model, code, SUM(qty) AS qty');
        $c->groupby('model');
        
        $stmt = $c->prepare();
        $stmt->execute();
        $rows = $stmt->fetchAll();
        
        $count = count($rows);
        


        It give me the expected result.
        • So, i just get back to work and tested the above code in my actual application.

          It give me the expected result (575), but il also seem to mess with getCollection after and don’t take in consideration the limit clause:

          <?php
          $c = $modx->newQuery($table);
          $c->select('id, model, SUM(failures) as failures');
          $c->groupBy('model');
          
          //Count return the right result here
          $t = $c->prepare();
          $t->execute();
          $rows = $t->fetchAll();
          $count = count($rows);
          
          //Seems like those properties are not taken into consideration
          $c->sortby($sort,$dir);
          $c->limit($limit, $start);
          $fields = $modx->getCollection($table, $c);
          


          I suppose it’s because i use execute?.
          • I’ve searched to do it with only one query, but it just doesn’t work with PDO.

            So now i’m using 2 queries:

            <?php
            
            //Retreive the count
            $c = $modx->newQuery($table);
            $c->groupBy('model');
            $stmt  = $c->prepare();
            $stmt->execute();
            $rows = $stmt->fetchAll();
            $count = count($rows);
            
            //Do the actual query
            $c = $modx->newQuery($table);
            $c->select('id, model, SUM(failures) as failures');
            $c->groupBy('model');
            $c->sortby($sort,$dir);
            $c->limit($limit, $start);
            $fields = $modx->getCollection($table, $c);
            


            Is there any more effective/elegant method to do this?
            • You are doing a GROUP BY model, so it looks like you want to select("COUNT(DISTINCT model)") here (don’t select id OR your sum() expression, or add the group by until you are actually getting the rows because grouping by model is the same as returning a single row per model); you’ll have to find the proper COUNT() expression to match the rows you are returning in the actual query, i.e.:
              <?php
              //Retreive the count
              $c = $modx->newQuery($table);
              $c->select("COUNT(DISTINCT model)");
              if ($c->prepare() && $c->stmt->execute()) {
                  $rows = $c->stmt->fetchAll(PDO::FETCH_COLUMN);
                  $count = (integer) reset($rows);
              
                  //Do the actual query only if there is a count
                  if ($count > 0) {
                      $c = $modx->newQuery($table);
                      $c->select('id, model, SUM(failures) as failures');
                      $c->groupBy('model');
                      $c->sortby($sort,$dir);
                      $c->limit($limit, $start);
                      $fields = $modx->getCollection($table, $c);
                  }
              }
              
              • Worked like a charm.

                I need to read more on PDO because even though you give me the solution, i don’t really understand why you use $c->stmt->fetchAll(PDO::FETCH_COLUMN) instead of fetchAll() alone.

                Thank you Jason.
                • Quote from: lossendae at Mar 17, 2010, 03:11 PM

                  I need to read more on PDO because even though you give me the solution, i don’t really understand why you use $c->stmt->fetchAll(PDO::FETCH_COLUMN) instead of fetchAll() alone.
                  PDO::FETCH_COLUMN is an option to return the results only from a single column; I didn’t specify the column here because I just want the first column, which is the default. Since I only want the result from the count, which is the first and only column in my result set, I’m just using this so I don’t have to fetchAll(PDO::FETCH_NUM), reset($rows) and then get the count from $row[0]; fewer steps if I just return a single column and grab the only result with a simple reset($rows).
                  • Useful informations, thank you again.