将数据绑定到Laravel中的DB :: raw()

时间:2014-10-07 21:37:12

标签: php mysql laravel-4 eloquent

我在原始SQL中编写了一个查询,用于提取给定日期范围内的所有记录,根据插入日期和小时计算记录,并计算自插入“开始日期”以来的小时数。

我现在正试图将该查询填入Laravel。我遇到的问题是我需要一些我在Eloquent文档中找不到的东西,所以我不得不使用DB::raw()来完成它们。这适用于硬编码值,但后来我得到了我需要使用动态值的点,这导致我this question。这个问题几乎可以解决,但我遇到的问题是,因为我在变量中使用了where子句,所以会产生一些奇怪的副作用。

例如,使用此查询:

$clicks = Tracker::select(DB::raw("TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'?'))/60/60+1 as hours_since_send"), DB::raw('COUNT(*)'))
    ->where('actual_date', '>', $start_date)
    ->where('actual_date', '<', $end_date)
    ->whereIn('link_id', $link_ids)
    ->groupBy(DB::raw('DAY(actual_date)'))
    ->groupBy(DB::raw('HOUR(actual_date)'))
    ->orderBy('actual_date', 'asc')
    ->setBindings([$start_date])
    ->get();

给我这个错误:

SQLSTATE[HY093]: Invalid parameter number (SQL: select TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'2014-10-02 00:00:00'))/60/60+1 as hours_since_send, COUNT(*) from `trackers` where `actual_date` > ? and `actual_date` < ? and `link_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) group by DAY(actual_date), HOUR(actual_date) order by `actual_date` asc)

这似乎很直接,所以我尝试了:

$clicks = Tracker::select(DB::raw("TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'?'))/60/60+1 as hours_since_send"), DB::raw('COUNT(*)'))
    ->where('actual_date', '>', $start_date)
    ->where('actual_date', '<', $end_date)
    ->whereIn('link_id', $link_ids)
    ->groupBy(DB::raw('DAY(actual_date)'))
    ->groupBy(DB::raw('HOUR(actual_date)'))
    ->orderBy('actual_date', 'asc')
    ->setBindings(array_merge([$start_date], $link_ids))
    ->get();

这给了我错误:

SQLSTATE[HY093]: Invalid parameter number (SQL: select TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'2014-10-02 00:00:00'))/60/60+1 as hours_since_send, COUNT(*) from `trackers` where `actual_date` > 1156 and `actual_date` < 1157 and `link_id` in (1158, 1159, 1160, 1161, 1162, 1163, 1164, 1165, 1166, 1167, 1168, 1169, 1170, 1171, ?, ?) group by DAY(actual_date), HOUR(actual_date) order by `actual_date` asc)

所以似乎setBindings只是覆盖了laravel在后台设置的绑定。我确实尝试用这个填充它们:

$clicks = Tracker::select(DB::raw("TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'?'))/60/60+1 as hours_since_send"), DB::raw('COUNT(*)'))
    ->where('actual_date', '>', $start_date)
    ->where('actual_date', '<', $end_date)
    ->whereIn('link_id', $link_ids)
    ->groupBy(DB::raw('DAY(actual_date)'))
    ->groupBy(DB::raw('HOUR(actual_date)'))
    ->orderBy('actual_date', 'asc')
    ->setBindings(array_merge([$start_date, $start_date, $end_date], $link_ids))
    ->get();

虽然不会抛出错误,但它不会返回任何数据。

所以,我的问题是:如何在这样的查询中使用带有绑定参数的DB::raw表达式,而不会弄乱后台的laravel集?或者我如何重写此查询以完成我需要的内容?

以下是供参考的原始原始查询:

SELECT
  TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'2014-10-02 00:00:00'))/60/60+1 as hours_since_send,
  COUNT(*)
FROM
  trackers
WHERE
  link_id BETWEEN 1156 AND 1171
  AND
  actual_date > '2014-10-02 00:00:00'
  AND actual_date < '2014-10-08 00:00:00'
GROUP BY
  DAY(actual_date),
  HOUR(actual_date)
ORDER BY
  actual_date

2 个答案:

答案 0 :(得分:3)

不要使用setBindings,因为您不需要覆盖所有where绑定(这就是您所做的),只需执行此操作:

$clicks = Tracker::select(DB::raw("TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),?))/60/60+1 as hours_since_send"), DB::raw('COUNT(*)'))
    ->addBinding($start_date, 'select')
    ->where('actual_date', '>', $start_date)
    ...

答案 1 :(得分:1)

Eloquent实际上有一个whereBetween方法,它可以接受两个Carbon实例(以及正常时间戳)并比较它们。

Tracker::whereBetween('actual_date', [$start, $end])->get(); 

http://laravel.com/docs/4.2/queries

相关问题