【发布时间】:2014-08-11 23:31:40
【问题描述】:
我正在尝试使用 Euromark 的地理编码行为(他的工具插件的一部分)在我的 CakePHP 2.4.3 应用程序中获取附近的帖子,并且遇到了 SQL 错误。这是我的控制器代码:
$this->Post->Behaviors->attach('Tools.Geocoder');
$this->Post->setDistanceAsVirtualField(44, 50); //or whatever coords
$options = array(
'contain' => array(), //same error when I don't use this, just using it to keep the SQL query easier to read
'order' => array('Post.distance' => 'ASC', 'limit' => 10)
);
$posts = $this->Post->find('all', $options);
$this->set('posts', $posts);
这会引发 SQL 错误;它说语法有问题:
SELECT
`Post`.`id`, `Post`.`lat`, `Post`.`lng`, `Post`.`body`,
(6371.04 * ACOS(
COS(
PI() / 2 - RADIANS(90 - `Post`.`lat`)
) * COS(
PI() / 2 - RADIANS(90 - 44)
) * COS(
RADIANS(`Post`.`lng`) - RADIANS(50)
) + SIN(
PI() / 2 - RADIANS(90 - `Post`.`lat`)
) * SIN(
PI() / 2 - RADIANS(90 - 44)
)
)) AS `Post__distance`
FROM `database`.`posts` AS `Post`
WHERE 1 = 1
ORDER BY
(6371.04 * ACOS(
COS(
PI() / 2 - RADIANS(90 - `Post`.`lat`)
) * COS(
PI() / 2 - RADIANS(90 - 44)
) * COS(
RADIANS(`Post`.`lng`) - RADIANS(50)
) + SIN(
PI() / 2 - RADIANS(90 - `Post`.`lat`)
) * SIN(
PI() / 2 - RADIANS(90 - 44)
)
)) ASC,
`limit` 10
我正在使用最新版本的插件,它说它适用于 Cake 2.x。我的帖子的地理位置数据存储为Post.lat 和Post.lng。任何想法为什么会创建格式错误的 SQL?我的 SQL 技能如此之高,我无法发现错误,大概是插件没问题,而且我的控制器操作有些问题。
【问题讨论】:
-
看起来它现在工作得很好:) PS:在较新的版本中,您可以使用 load() 而不是 attach()。后者已弃用。
标签: php sql cakephp cakephp-2.4