我正在尝试使用三个“方案”查询MYSQL表以查找对象。虽然我已经成功地将这些分解为三个单独的查询,但我觉得必须有一种“更好,更快”的方法来筛选数据。但是,当我像下面这样组合时,我找不到任何与查询匹配的对象。这是在MODx中使用xPDO。失败的尝试紧接在下面:
$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);
但是,如果我单独运行每个场景,它会正确地拾取对象。例如:
$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);
我在第一段代码中出错的任何想法?如果需要任何进一步的信息,请告诉我。干杯!
有用的文档:http://rtfm.modx.com/xpdo/2.x/class-reference/xpdoquery/xpdoquery.where
答案 0 :(得分:2)
在$orders->where()
方法中列出时,代码中的数组场景被视为AND条件。
试试这个:
$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);