【问题标题】:Laravel 5 whereRaw result an error but the returned query runs fine in postgres clientLaravel 5 whereRaw 导致错误,但返回的查询在 postgres 客户端中运行良好
【发布时间】:2015-08-24 21:33:34
【问题描述】:

我收到错误:SQLSTATE[42P18]: Indeterminate datatype: 7 ERROR: could not determine data type of parameter $2 主要问题在于我的那部分代码中的 whereRaw() 函数:

$result = $result->whereRaw(
    "lower(translate(?, ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', ' -aaaaaeeeeiiiiooooouuuucc')) LIKE lower(translate('%?%', ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', ' -aaaaaeeeeiiiiooooouuuucc'))",
    [
        $field_where,
        $value
    ]
);

但是当我得到 结果查询 并在我的 postgres 客户端中运行时,我没有收到错误。

那么……有什么问题?


异常

SQLSTATE[42P18]: Indeterminate datatype: 7 ERROR: could not 
determine data type of parameter $2 (SQL: select count(*) as 
aggregate from "pace_records" left join "customers" on 
"customers"."id" = "pace_records"."customer_id" left join "cities"
on "cities"."id" = "customers"."city_id" left join 
"customer_subregions" on "customer_subregions"."id" = 
"pace_records"."customer_subregion_id" left join "schools" on 
"schools"."id" = "pace_records"."school_id" left join "programs" on 
"programs"."id" = "pace_records"."program_id" left join "users" on 
"users"."id" = "pace_records"."user_id" where 
"pace_records"."deleted_at" is null and 
lower(translate("cities"."name", ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', ' -aaaaaeeeeiiiiooooouuuucc')) LIKE 
lower(translate('%Arapi%', ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', ' -aaaaaeeeeiiiiooooouuuucc')))

完整代码

$result = static::joins();

if (isset(self::$searchable[$field])) {
    $field_where = $field;
    if (isset(self::$searchable[$field]['join_field'])) {
        $field_where = self::$searchable[$field]['join_field'];
    }

    $result = $result->whereRaw(
        "lower(translate(?, ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', ' -aaaaaeeeeiiiiooooouuuucc')) LIKE lower(translate('%?%', ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', ' -aaaaaeeeeiiiiooooouuuucc'))",
        [
            $field_where,
            $value
        ]
    );
}

return $result->orderBy('cities.name', 'ASC')
    ->orderBy('schools.name', 'ASC')
    ->orderBy('programs.name', 'ASC')
    ->paginate($maxPerPage);

信息

  • Laravel 5.1.x
  • PHP 5.6.x
  • PostgreSQL 9.4
  • Mac OS 优胜美地 10.10.4

如果您需要更多信息,请告诉我。

【问题讨论】:

  • 也许您应该考虑使用 Postgres 的非重音扩展 postgresql.org/docs/9.4/static/unaccent.html ILIKE 运算符不区分大小写。
  • 第一个whereRaw只有一个参数,第二个问号用引号括起来。
  • 也许你的 preg_replace 没有返回你所期望的,因此向查询发送了无效的参数。
  • @greg 首先,感谢您的回答。请考虑 if 和 else (我删除它)。关于第一个whereRaw,如果你看第一个whereRaw参数的末尾,我把第二个参数放在了第二个lower(中。

标签: php postgresql laravel


【解决方案1】:

我目前的解决方案是连接值而不是使用参数。

像这样:

    if (self::$searchable[$field]['type'] == 'LIKE') {
        $result = $result->whereRaw("lower(translate($field_where, ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', ' -aaaaaeeeeiiiiooooouuuucc')) 
        LIKE lower(translate('%$value%', ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', ' -aaaaaeeeeiiiiooooouuuucc'))");
    } else {
        $result = $result->whereRaw("lower(translate($field_where, ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', ' -aaaaaeeeeiiiiooooouuuucc')) 
        = lower(translate($value, ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', ' -aaaaaeeeeiiiiooooouuuucc'))");
    }

有效,但我不喜欢这样做。

我会继续寻找其他解决方案。

【讨论】:

    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2022-12-15
    • 2012-12-05
    • 2017-06-30
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多