Jump to main content Jump to doc navigation

xPDO::getCount

Retrieves a count of xPDOObjects by the specified array or xPDOCriteria.

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.

Syntax

API Docs: https://api.modx.com/revolution/2.2/db_core_xpdo_xpdo.class.html#\xPDO::getCount()

integer getCount (string $className, [mixed $criteria = null])

Example

Get a count of all the Box objects with width 20.

$total = $xpdo->getCount('Box',array(
   'width' => 20,
));

Note that if you pass this function a query object for the second parameter, the limit criteria may be ignored.

$query = $modx->newQuery('States');
$query->limit(10, 0);  // <-- probably you want to put this line AFTER the getCount

$total_states = $modx->getCount('States',$query);

// If you have 50 states, this may print 50, not 10!  Be careful!
$modx->log(modX::LOG_LEVEL_ERROR, "Total States: $total_states");

See Also

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • Fabian Christen
  • Dannevang Digital
  • Sepia River Studios
  • Chris Fickling
  • CrewMark
  • deJaya
  • eydolan
  • Following Sea
  • Lefthandmedia
  • Murray Wood
  • Anton Tarasov
  • Stéphane Jäggi
  • Raffy
  • Snow Creative
  • Nick Clark
  • A. Moreno
  • JT Skaggs
  • Helen
  • YJ
  • krisznet
  • Richard
  • Yanni

Budget

$311 per month—let's make that $500!

Learn more