xPDOQuery
Last updated Dec 21st, 2019 | Page history | Improve this page | Report an issue
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
Budget
$306 per month—let's make that $500!
Learn moreThe xPDOQuery
extends the xPDOCriteria
class and allows you to abstract out complex SQL queries into an OOP format. This allows encapsulation of SQL calls so that they can work in multiple database types, and be easy to read and dynamically build.
- xPDOQuery.andCondition
- xPDOQuery.groupby
- xPDOQuery.innerJoin
- xPDOQuery.leftJoin
- xPDOQuery.limit
- xPDOQuery.orCondition
- xPDOQuery.rightJoin
- xPDOQuery.select
- xPDOQuery.setClassAlias
- xPDOQuery.sortby
- xPDOQuery.where
Examples¶
Grab the first 4 Boxes with:
- Owners that have the letter 'a' in their names
- A width of at least 10
- A height that is not 2
- A color of 'red','blue' or 'green'
- sorted by the Box name, ascending and then by the Box height, descending
$query = $xpdo->newQuery('Box');
// Remember: syntax here is classname, your alias. Note that filters use the alias.
$query->innerJoin('Owner','User');
// the Owner is actually a User object, defined as Owner in the relationship alias
$query->where(array(
'Owner.name:LIKE' => '%a%',
'Box.width:>=' => 10,
'Box.height:!=' => 2,
'Box.color:IN' => array('red','green','blue'),
));
$query->sortby('Box.name','ASC');
$query->sortby('Box.height','DESC');
$query->limit(4);
$boxes = $xpdo->getCollection('Box',$query);
You can also do more complex queries, like so:
$query = $xpdo->newQuery('Person');
$query->where(array(
array(
'first_name:=' => 'Bob',
array(
'OR:last_name:LIKE' => 'Boblablaw',
'AND:gender:=' => 'M',
),
),
'password:!=' => null,
));
translates to:
(
( `Person`.`first_name` = 'Bob'
OR ( `Person`.`last_name` LIKE 'Boblablaw' AND `Person`.`gender` = 'M' )
)
AND password IS NOT NULL
)
Note that if you're specifying the conditional in the key string, such as 'OR:disabled:!=' => true, you'll need to specify the operand as well. This means that you must specify = explicitly, such as in: 'AND:gender:=' => 'M'
Valid Operators¶
$c = $xpdo->newQuery('Person');
$c->where(array(
'name:=' => 'John', /* Equal To */
'name:!=' => 'Sue', /* Unequal To */
'age:>' => '21', /* Greater Than */
'age:>=' => '21', /* Greater Than or Equal To */
'age:<' => '18', /* Less Than */
'age:<=' => '18', /* Less Than or Equal To */
'search:LIKE' => 'Term', /* LIKE statement */
'field' => null, /* check for NULL */
'ids:IN' => array(1,2,3), /* IN statement */
));
Debugging¶
Sometimes you need to see what query is actually being generated. You can do this by preparing the query and outputting it using the toSQL() method.
$c = $xpdo->newQuery('Person');
// add filters here...
$c->prepare();
print $c->toSQL();
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
Budget
$306 per month—let's make that $500!
Learn more