GroupBy 和日期
GroupBy and date
我正在尝试获取每一天的预订量。我已经完成了这个学说要求:
// prepare date for request
$today = new DateTime('now');
$lastMonth = new DateTime('-30 days');
$qb
->select('r.reservationDate, sum(r.totalAmountNoTaxes) as totalAmountNoTaxes')
->where('r.companyId = :companyId')
->setParameter('companyId', $companyId)
->andWhere('r.reservationDate BETWEEN :month AND :today')
->setParameter('month', $lastMonth->format('Y-m-d'))
->setParameter('today', $today->format('Y-m-d'))
->orderBy('r.reservationDate', 'ASC')
->groupBy('r.reservationDate')
;
我得到了这样的结果(来自我的网络服务)
{
"reservationDate": "2016-12-21T07:41:12+0000",
"totalAmountNoTaxes": "26150.30"
},
{
"reservationDate": "2016-12-22T07:41:01+0000",
"totalAmountNoTaxes": "230.00"
},
{
"reservationDate": "2016-12-22T07:41:02+0000",
"totalAmountNoTaxes": "21966.40"
},
{
"reservationDate": "2016-12-23T07:40:52+0000",
"totalAmountNoTaxes": "20407.05"
},
{
"reservationDate": "2016-12-24T07:40:41+0000",
"totalAmountNoTaxes": "4319.75"
},
{
"reservationDate": "2016-12-24T07:40:42+0000",
"totalAmountNoTaxes": "7261.20"
},
{
"reservationDate": "2016-12-25T07:40:28+0000",
"totalAmountNoTaxes": "14821.45"
},
{
"reservationDate": "2016-12-26T07:40:16+0000",
"totalAmountNoTaxes": "494.50"
},
{
"reservationDate": "2016-12-26T07:40:17+0000",
"totalAmountNoTaxes": "14541.25"
},
{
"reservationDate": "2016-12-27T07:06:40+0000",
"totalAmountNoTaxes": "25763.90"
},
{
"reservationDate": "2016-12-28T05:37:43+0000",
"totalAmountNoTaxes": "20595.70"
}
问题是有时日期分组不正确。例如,将 2016-12-22 拆分为 2。与 24、26 相同。
我该怎么办?
您的数据库中似乎有日期时间字段。
解决方案将按日期格式分组:
//
->groupBy('DATE_FORMAT(r.reservationDate, '%Y%m%d')')
//
但是 DATE_FORMAT isnt implemented in default doctrine bundle, so you will have to add beberlei/DoctrineExtensions 然后将相应的函数添加到您的学说包配置中,例如:
doctrine:
dbal:
....
orm:
....
dql:
string_functions:
DATE_FORMAT: DoctrineExtensions\Query\Mysql\DateFormat
我正在尝试获取每一天的预订量。我已经完成了这个学说要求:
// prepare date for request
$today = new DateTime('now');
$lastMonth = new DateTime('-30 days');
$qb
->select('r.reservationDate, sum(r.totalAmountNoTaxes) as totalAmountNoTaxes')
->where('r.companyId = :companyId')
->setParameter('companyId', $companyId)
->andWhere('r.reservationDate BETWEEN :month AND :today')
->setParameter('month', $lastMonth->format('Y-m-d'))
->setParameter('today', $today->format('Y-m-d'))
->orderBy('r.reservationDate', 'ASC')
->groupBy('r.reservationDate')
;
我得到了这样的结果(来自我的网络服务)
{
"reservationDate": "2016-12-21T07:41:12+0000",
"totalAmountNoTaxes": "26150.30"
},
{
"reservationDate": "2016-12-22T07:41:01+0000",
"totalAmountNoTaxes": "230.00"
},
{
"reservationDate": "2016-12-22T07:41:02+0000",
"totalAmountNoTaxes": "21966.40"
},
{
"reservationDate": "2016-12-23T07:40:52+0000",
"totalAmountNoTaxes": "20407.05"
},
{
"reservationDate": "2016-12-24T07:40:41+0000",
"totalAmountNoTaxes": "4319.75"
},
{
"reservationDate": "2016-12-24T07:40:42+0000",
"totalAmountNoTaxes": "7261.20"
},
{
"reservationDate": "2016-12-25T07:40:28+0000",
"totalAmountNoTaxes": "14821.45"
},
{
"reservationDate": "2016-12-26T07:40:16+0000",
"totalAmountNoTaxes": "494.50"
},
{
"reservationDate": "2016-12-26T07:40:17+0000",
"totalAmountNoTaxes": "14541.25"
},
{
"reservationDate": "2016-12-27T07:06:40+0000",
"totalAmountNoTaxes": "25763.90"
},
{
"reservationDate": "2016-12-28T05:37:43+0000",
"totalAmountNoTaxes": "20595.70"
}
问题是有时日期分组不正确。例如,将 2016-12-22 拆分为 2。与 24、26 相同。
我该怎么办?
您的数据库中似乎有日期时间字段。 解决方案将按日期格式分组:
//
->groupBy('DATE_FORMAT(r.reservationDate, '%Y%m%d')')
//
但是 DATE_FORMAT isnt implemented in default doctrine bundle, so you will have to add beberlei/DoctrineExtensions 然后将相应的函数添加到您的学说包配置中,例如:
doctrine:
dbal:
....
orm:
....
dql:
string_functions:
DATE_FORMAT: DoctrineExtensions\Query\Mysql\DateFormat