Home > Backend Development > PHP Tutorial > Detailed explanation of query examples of Symfony2 based on input time

Detailed explanation of query examples of Symfony2 based on input time

小云云
Release: 2023-03-20 08:30:02
Original
1301 people have browsed it

Generally: when the front-end inputs a time, we usually modify the time into a timestamp firststrtotime - Parse the date and time description of any English text into a Unix timestamp. This article mainly introduces it to you. Symfony2's method of querying the input time, combined with examples, analyzes the related operating techniques of Symfony2 for converting and querying the input time of mysql and MongoDB. Friends who need it can refer to it. I hope it can help everyone.

For example:


$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
Copy after login

Then: If it is just time, in order to prevent the time passed by others from being false, you need to modify the time into the form of Y-m-d


$start = date('Y-m-d 00:00:00', $startTimestamp);
$end = date('Y-m-d 23:59:59', $endTimestamp);
Copy after login

1. Use in 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();
Copy after login

2. In mongodb There are actually two pitfalls in the time input and query examples in

:


##

@ ->field('submit_time')->gt(new \DateTime($start))
->field('submit_time')->lt(new \DateTime($end))
Copy after login

Here, for time query, For greater than and less than, an object must be passed.


$query->field('status')->equals($status);
Copy after login

Here, mongodb will not help you identify this as an int type by default. What type it is, it must be passed in manually.


$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();
Copy after login
Related recommendations:

Detailed explanation of the usage of Symfony2 framework form

Detailed explanation of Symfony2 implementation of union Query method

Detailed explanation of Symfony2 input time for query

The above is the detailed content of Detailed explanation of query examples of Symfony2 based on input time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template