Symfony2时间查询方法概述
本示例详细解析了如何在Symfony2框架下实现基于输入时间的查询功能,供开发者参考借鉴。
通常场景下,当接收到前端传递的时间参数时,首要步骤是将其转换为Unix时间戳格式。
PHP内置函数strtotime可用于将英文文本描述的日期时间字符串解析为标准Unix时间戳。
具体转换逻辑如下代码所示:
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
若仅涉及日期部分,为确保数据真实性,建议统一将时间格式化为Y-m-d形式进行后续处理。
$start = date('Y-m-d 00:00:00', $startTimestamp);
$end = date('Y-m-d 23:59:59', $endTimestamp);
1. MySQL数据库中的时间查询实现
if(empty($startDate)) {
throw new \Exception('起始时间不为空', BaseException::ERROR_CODE_ILLEGAL_PARAMETER);
}
if (empty($endDate)) {
$endDate = $startDate;
}
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
if ($startTimestamp > $endTimestamp) {
throw new \Exception('时间参数错误', BaseException::ERROR_CODE_ILLEGAL_PARAMETER);
}
if ($status == InventoryOrder::STATUS_SUBMITTED) {
$index = 'i.submitTime';
} else if ($status == InventoryOrder::STATUS_UNSUBMITTED) {
$index = 'i.createTime';
} else if (empty($status)) {
$index = 'i.createTime';
} else {
throw new \Exception('时间格式不正确', BaseException::ERROR_CODE_ILLEGAL_PARAMETER);
}
$sql = 'SELECT i FROM AppBundle:InventoryOrder i WHERE ';
$sql .= $index;
$sql .= ' BETWEEN :startDate AND :endDate ';
$start = date('Y-m-d 00:00:00', $startTimestamp);
$end = date('Y-m-d 23:59:59', $endTimestamp);
$params['endDate'] = $end;
$params['startDate'] = $start;
if (!empty($status)) {
$sql .= ' AND i.status = :status';
$params['status'] = $status;
}
$sql .=' ORDER By i.createTime DESC';
$query = $this->entityManager->createQuery($sql);
$orderList = $query->setParameters($params)->getResult();
2. MongoDB数据库的时间输入与查询示例
在此过程中存在两个常见陷阱需要注意:
@ ->field('submit_time')->gt(new \DateTime($start))
->field('submit_time')->lt(new \DateTime($end))
对于MongoDB中的时间范围查询,大于(gt)和小于(lt)操作符必须接收对象类型的参数。
$query->field('status')->equals($status);
MongoDB驱动不会自动推断时间参数的数据类型,因此必须显式地手动传入相应的对象实例。
$data = array();
if (!isset($startDate)) {
throw new \Exception("参数不正确", BaseException::ERROR_CODE_ILLEGAL_PARAMETER);
}
if (empty($endDate)) {
$endDate = $startDate;
}
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
if ($startTimestamp > $endTimestamp) {
throw new \Exception("参数不正确", BaseException::ERROR_CODE_ILLEGAL_PARAMETER);
}
$start = date('Y-m-d 00:00:00', $startTimestamp);
$end = date('Y-m-d 23:59:59', $endTimestamp);
$scanner = Order::FROM_TYPE_SCANNER;
$query = $this->documentManager
->createQueryBuilder('AppBundle:Order')
->field('submit_time')->gt(new \DateTime($start))
->field('submit_time')->lt(new \DateTime($end))
->field('from_type')->equals("$scanner");
if (!empty($status) && in_array($status, array(Order::STATUS_CANCELLED, Order::STATUS_SUBMITTED))) {
$status = $status + 0;
$query->field('status')->equals($status);
} else if (empty($status)) {
$status = Order::STATUS_SUBMITTED + 0;
$query->field('status')->equals($status);
} else {
throw new \Exception("参数不正确", BaseException::ERROR_CODE_ILLEGAL_PARAMETER);
}
$orderList = $query->sort('create_time', 'DESC')
->getQuery()
->execute();
希望上述内容能帮助掌握Symfony2框架下的PHP程序设计技巧。