Jump to main content Jump to doc navigation

xPDO\Om\xPDOExpression is an immutable value object that wraps a raw SQL fragment so xPDO can inline it verbatim instead of binding it as a parameter. Use it wherever xPDOQuery accepts a column value or a SELECT entry when you need a SQL expression — a function call (NOW(), CURRENT_TIMESTAMP), an arithmetic expression (counter + 1), or an aggregate with an alias (COUNT(*) AS total). Valid contexts include SET, SELECT, WHERE, HAVING, GROUP BY, and ORDER BY.

The expression is embedded without quoting, escaping, or parameterisation, so keeping it safe is the caller's responsibility.

[!WARNING] Never pass unsanitised user input to xPDOExpression or to xPDO::expression(). The string is inserted into the generated SQL as-is and can lead to SQL injection.

Creating an Expression

The class is final; build instances with the xPDO::expression() factory:

$expr = $xpdo->expression('NOW()');

Examples

Sort by a raw expression:

$query = $xpdo->newQuery('modResource');
$query->sortby($xpdo->expression('COUNT(*)'), 'DESC');

Use an expression as a bound value in WHERE:

$query = $xpdo->newQuery('modResource');
$query->where(array(
    'createdon:<=' => $xpdo->expression('NOW()'),
));

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
  • CrewMark
  • Fabian Christen
  • Sepia River Studios
  • Dannevang Digital
  • Alex
  • A. Moreno
  • Chris Fickling
  • Stéphane Jäggi
  • Murray Wood
  • Anton Tarasov
  • JT Skaggs
  • deJaya
  • Lefthandmedia
  • eydolan
  • Following Sea
  • Guido Gallenkamp
  • YJ
  • Raffy
  • Snow Creative
  • Nick Clark
  • Guest
  • Helen
  • krisznet
  • Yanni
  • Richard

Budget

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

Learn more