If you are specifying the select(), don’t use getCount(), just run the query and get the results normally. getCount() is a shortcut that replaces your select() with COUNT(DISTINCT primaryKeyField) automatically, based on the primary key definition of the class you specify. Group by should work, as long as it makes sense with the COUNT(DISTINCT primaryKeyField) select clause.
I was wondered how to use getCount properly.
Whenever i use a groupBy or select(DISTINCT... statement, getCount always return a wrong value.
Is there any proper way to use it with those properties?
<?php
$c = $modx->newQuery($table);
$c->select('id, model, SUM(failures) as failures, updatedon');
$c->groupBy('model');
$c->limit($limit, $start);
$c->sortby($sort,$dir);
<?php $c = $modx->newQuery($table); $c->select('id, model, SUM(failures) as failures, updatedon'); $c->groupBy('model'); $c->limit($limit, $start); $c->sortby($sort,$dir);
Where should i put a COUNT statement to have the total result of the query?
<?php
$c = $modx->newQuery($table);
$c->groupBy('model');
$count = $modx->getCount($table,$c);
$c->select('id, model, SUM(failures) as failures, updatedon');
$c->limit($limit, $start);
$c->sortby($sort,$dir);
$collection = $modx->getCollection($table,$c);
I tested this combination, but $count return 4 instead of 575.
Is there any way to debug xPDO to see where the problem might be? Or maybe it’s a PDO issue with Wamp?
<?php <object class="Model" table="model" extends="xPDOSimpleObject"> <field key="model" dbtype="varchar" precision="50" phptype="string" null="false" index="model"/> <field key="description" dbtype="varchar" precision="200" phptype="string" null="false"/> <field key="qty" dbtype="int" precision="11" attributes="unsigned" phptype="integer" null="false" default="0"/> <field key="failures" dbtype="int" precision="11" attributes="unsigned" phptype="integer" null="false" default="0" index="failures"/> <field key="updatedon" dbtype="datetime" phptype="datetime" null="true"/> <field key="updatedby" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0"/> </object>
echo $c->toSql();
SELECT `Model`.`id` AS `Model_id`, `Model`.`model` AS `Model_model`, `Model`.`description` AS `Model_description`, `Model`.`qty` AS `Model_qty`, `Model`.`failures` AS `Model_failures`, `Model`.`updatedon` AS `Model_updatedon`, `Model`.`updatedby` AS `Model_updatedby` FROM `test_model` AS `Model` GROUP BY model