Reputation: 5598
I am attempting to query a MYSQL table with three 'scenarios' for finding objects. While I have successfully broken these into three separate queries I feel there has to be a 'better and faster' way to sift through the data. However, when I combine like below, I do not find any objects matching the query. This is using xPDO within MODx. The failed attempt is immediately below:
$orders=$modx->newQuery('Orders');
$orders->where(array(
array( //scenario #1
'Orders.start_date:<=' => $rentalDate->end_date,
'AND:Orders.start_date:>=' => $rentalDate->start_date
),
array( //scenario #2
'OR:Orders.end_date:<=' => $rentalDate->end_date,
'AND:Order.start_date:>=' => $rentalDate->start_date
),
array( //scenario #3
'OR:Orders.end_date:>=' => $rentalDate->start_date,
'AND:Orders.end_date:<=' => $rentalDate->end_date
)
));
$conflictingOrders = $modx->getCollection('Orders',$orders);
However, if I run each scenario separately, it does pick up the objects correctly. Example:
$s1Query=$modx->newQuery('Orders');
$s1Query->where(array(array('Orders.start_date:<=' => $rentalDate->end_date,'AND:Orders.start_date:>=' => $rentalDate->start_date)));
$s1Results=$modx->getCollection('Orders',$s1Query);
Any ideas where I am going wrong in the first code? Please let me know if any further information is needed. Cheers!
Helpful doc:http://rtfm.modx.com/xpdo/2.x/class-reference/xpdoquery/xpdoquery.where
Upvotes: 1
Views: 83
Reputation: 3146
The array scenarios in your code are being treated as AND conditions when listed in the $orders->where()
method.
Try this:
$orders = $modx->newQuery('Orders');
$orders->where(array(
'Orders.start_date:<=' => $rentalDate->end_date,
'AND:Orders.start_date:>=' => $rentalDate->start_date
));
$orders->orCondition(array( //scenario #2
'Orders.end_date:<=' => $rentalDate->end_date,
'AND:Order.start_date:>=' => $rentalDate->start_date
));
$orders->orCondition(array( //scenario #3
'Orders.end_date:>=' => $rentalDate->start_date,
'AND:Orders.end_date:<=' => $rentalDate->end_date
));
// uncomment the following lines to see the raw query generated
// $orders->prepare();
// print_r($orders->toSql());
$conflictingOrders = $modx->getCollection('Orders',$orders);
Upvotes: 2