xPDOQuery.orCondition
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
$311 per month—let's make that $500!
Learn morexPDOQuery::orCondition¶
Add an OR condition to the WHERE statement in the query.
Syntax¶
API Docs: xPDOQuery::orCondition()
void orCondition ( $conditions, [ $binding = null], [ $group = 0])
Example¶
Grab all boxes with width 12 or 14.
$query = $xpdo->newQuery('Box');
$query->where(array(
'width' => 14,
));
$query->orCondition(array(
'width' => 12,
));
$boxes = $xpdo->getCollection('Box',$query);
Warning¶
The order you call the functions is important! The orCondition must come after the where method has been used.
Another Example¶
Here's a more familiar example used to retrieve pages when they've got publish/unpublish dates set. This demonstrates an alternative syntax for an or condition. Normally, each place in the array supplied to the where method is joined together by a SQL "AND", but you can use the "OR" prefix on your column names to specify how groups of terms are joined together.
In the following example, a page must be published (1), and the pub_date must be either zero OR less than or equal to the current timestamp. The unpub_date must be either zero OR greater than current timestamp.
$criteria = $modx->newQuery('modResource');
$criteria->where(array(
'published' => 1,
array(
'pub_date' => 0,
'OR:pub_date:<=' => time(),
),
array(
'unpub_date' => 0,
'OR:unpub_date:>' => time(),
),
)
);
Example With Joined Tables¶
Your filter parameters can reference fields in other tables.
$query = $modx->newQuery('modUser');
$query->innerJoin('modUserProfile','Profile');
$query->where(array(
'modUser.username' => $email,
));
$query->orCondition(array(
'Profile.email' => $email,
));
$user = $modx->getObject('modUser', $query);
The filter parameters may use the class name (as in modUser above) or the alias (as the Profile above).
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
$311 per month—let's make that $500!
Learn more