gpt4 book ai didi

postgresql - CakePHP 在 WHERE 子句中为函数名加上引号

转载 作者:行者123 更新时间:2023-11-29 12:06:57 26 4
gpt4 key购买 nike

我正在使用 PostgreSQL 中 earthdistance 模块的各种函数,其中之一是 ll_to_earth。给定纬度/经度组合,我尝试通过 CakePHP 1.2.5 Stable 从我的数据库中检索最近的点。

// set dummy data
$latitude = 30.4393696;
$longitude = -97.6200043;

// create a simple bounding box in the WHERE conditions, sort the points by location, and retrieve the nearest point
$location = $this->Location->find('first', array(
'fields' => array(
'id',
'coords',
"earth_distance(coords, ll_to_earth($latitude, $longitude)) AS distance",
),
'conditions' => array(
'coords >=' => 'll_to_earth(' . ($latitude - 0.5) . ', ' . ($longitude - 0.5) . ')',
'coords <=' => 'll_to_earth(' . ($latitude + 0.5) . ', ' . ($longitude + 0.5) . ')',
),
'order' => array('distance ASC'),
));

SQL 输出(为了便于阅读而格式化)最终看起来像这样:

SELECT
"Location"."id" AS "Location__id",
"Location"."coords" AS "Location__coords",
earth_distance(coords, ll_to_earth(30.4393696, -97.6200043)) AS distance FROM "locations" AS "Location"
WHERE
"coords" >= 'll_to_earth(29.9393696, -97.1200043)' AND
"coords" <= 'll_to_earth(30.9393696, -96.1200043)'
ORDER BY
"distance" ASC
LIMIT 1

您可以看出我的问题所在:CakePHP 正在引用我的查找条件,包括函数名称 ll_to_earth。我是否只需要通过模型的 query 方法手动查询,或者是否有办法告诉 Cake ll_to_earth 是一个 DB 函数并且不引用它?

最佳答案

你应该可以这样做:

$location = $this->Location->find('first', array(
'fields' => array(
'id',
'coords',
"earth_distance(coords, ll_to_earth($latitude, $longitude)) AS distance",
),
'conditions' => array(
'coords >= ll_to_earth(' . ($latitude - 0.5) . ', ' . ($longitude - 0.5) . ')
and coords <= ll_to_earth(' . ($latitude + 0.5) . ', ' . ($longitude + 0.5) . ')'),
'order' => array('distance ASC'),
));

但这也将绕过 Cake 对输入所做的所有清理,因此您必须自己处理。

关于postgresql - CakePHP 在 WHERE 子句中为函数名加上引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1579258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com