【问题标题】:Bind data to DB::raw() in Laravel在 Laravel 中将数据绑定到 DB::raw()
【发布时间】:2014-12-02 10:55:53
【问题描述】:

我用原始 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();

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

所以,我的问题是:如何在这样的查询中使用我的 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

【问题讨论】:

    标签: php mysql laravel-4 eloquent


    【解决方案1】:

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

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

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

    【讨论】:

    • 好吧,这将代码缩短了一行,但由于传入的参数数量与以前相同,我仍然收到 Invalid parameter number 错误。
    • 对不起,我原来的答案有错误的参数。你试过更新的吗?列的字符串,然后是范围的数组?
    • 是的。问题是setBindings 正在尝试为整个查询中的所有参数设置参数。将两个 where 更改为一个 whereBetween 不会更改参数计数。
    • 注:whereBetween等于x &gt;= 1 AND x &lt;= 2,这里有&lt;&gt;,所以不一样。
    • 这很容易。非常感谢!
    【解决方案2】:

    不要使用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)
        ...
    

    【讨论】:

    • 这怎么知道应该在哪里添加绑定?
    • 因为它是明确指定的-select
    • 明白了。我添加了这个,经过一个小的调整后它确实起作用了——需要从? 周围删除引号。谢谢!
    • 是的,? 周围不应该有任何引用。这是你的例子中的 c/p
    猜你喜欢
    • 2019-08-20
    • 2017-06-14
    • 2018-01-24
    • 2018-11-17
    • 2018-10-24
    • 2015-03-26
    • 2019-01-10
    • 2019-03-23
    • 2016-07-29
    相关资源
    最近更新 更多