我一直在浪费太多时间来解决与Phalcon/Paginator
QueryBuilder
一起用于特定查询的问题。查询工作正常,直到我尝试添加分页器,似乎分页器打破了查询。看到Phalcon中内置了一个分页器,我在整个项目中都使用过它,通过创建一个单独的自定义分页系统来解决这个问题似乎是错误的!所以我想让它发挥作用。
这是建设者;
$_ads = $this->modelsManager->createBuilder()
->columns($dist . ' AS distance, classifieds_ads.id AS id, classifieds_ads.title AS title, classifieds_ads.description AS description, classifieds_ads.city AS city, classifieds_ads.country AS country, classifieds_ads.latitude AS latitude, classifieds_ads.longitude AS longitude, classifieds_images.file AS image')
->addFrom('\Baseapp\Models\ClassifiedsAds', 'classifieds_ads')
->leftJoin('\Baseapp\Models\ClassifiedsImages', "classifieds_images.ad_id = classifieds_ads.id", 'classifieds_images')
->where($where)
->groupBy('classifieds_ads.id')
->having($having)
->orderBy($order);
以下是没有分页器生成的查询的示例;
SELECT (3959 * acos(cos(radians(****)) * cos(radians(`classifieds_ads`.`latitude`)) * cos(radians(`classifieds_ads`.`longitude`) - radians(****)) + sin(radians(****)) * sin(radians(`classifieds_ads`.`latitude`)))) AS `distance`, `classifieds_ads`.`id` AS `id`, `classifieds_ads`.`title` AS `title`, `classifieds_ads`.`description` AS `description`, `classifieds_ads`.`city` AS `city`, `classifieds_ads`.`country` AS `country`, `classifieds_ads`.`latitude` AS `latitude`, `classifieds_ads`.`longitude` AS `longitude`, `classifieds_images`.`file` AS `image`
FROM `classifieds_ads` AS `classifieds_ads`
LEFT JOIN `classifieds_images` AS `classifieds_images` ON `classifieds_images`.`ad_id` = `classifieds_ads`.`id`
WHERE `classifieds_ads`.`status` = 'active' AND (`title` LIKE '%query%' OR `description` LIKE '%query%' OR `classifieds_ads`.`tags` LIKE '%query%')
GROUP BY `id`
HAVING `distance` <= 200
ORDER BY IF(`title` LIKE '%query%', 1, IF(`classifieds_ads`.`tags` LIKE '%query%', 2, IF(`description` LIKE '%query%', 3, 4)))
这可以按预期工作。但是,当我添加分页器时;
$paginator = new \Phalcon\Paginator\Adapter\QueryBuilder(
array(
'builder' => $_ads,
'limit' => 5,
'page' => \Phalcon\DI::getDefault()->getShared('request')->getQuery('page', 'int')
)
);
$ads = $paginator->getPaginate();
这给了我一个错误:PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064....right syntax to use near ') AS
rowcount FROM
paginator现在生成的查询是;
SELECT COUNT() AS `rowcount`
FROM `classifieds_ads` AS `classifieds_ads`
LEFT JOIN `classifieds_images` AS `classifieds_images` ON `classifieds_images`.`ad_id` = `classifieds_ads`.`id`
WHERE `classifieds_ads`.`status` = 'active' AND (`classifieds_ads`.`title` LIKE '%query%' OR `classifieds_ads`.`description` LIKE '%query%' OR `classifieds_ads`.`tags` LIKE '%query%')
HAVING `classifieds_ads`.`distance` <= 200
似乎SELECT COUNT()
打破了查询,而且查询现在还不完整!我觉得这可能是因为我在SELECT table.field AS xxx
使用了columns()
。
我不确定这种意想不到的行为是否是一个错误,或者我是否会忽略某些事情。有什么想法吗?
答案 0 :(得分:0)
$_ads = $this->modelsManager->createBuilder()
->columns([
'distance' => $dist,
'id' => 'classifieds_ads.id',
'title' => 'classifieds_ads.title',
'description' => 'classifieds_ads.description',
'city' => 'classifieds_ads.city',
'country' => 'classifieds_ads.country',
'latitude' => 'classifieds_ads.latitude',
'longitude' => 'classifieds_ads.longitude',
'image' => 'classifieds_images.file',
])
->addFrom('\Baseapp\Models\ClassifiedsAds', 'classifieds_ads')
->leftJoin('\Baseapp\Models\ClassifiedsImages', "classifieds_images.ad_id = classifieds_ads.id", 'classifieds_images')
->where($where)
->groupBy('classifieds_ads.id')
->having($having)
->orderBy($order);
<强>更新强>
执行类似查询时,我似乎得到完全相同的错误
SQLSTATE [42000]:语法错误或访问冲突:1064您有 SQL语法错误;查看与您的手册相对应的手册 MySQL服务器版本,用于在')AS
rowcount
附近使用正确的语法 来自第1行的order
'
我能够使用解决方法来解决这个问题。而不是使用QueryBuilder作为数据:
$paginator = new Phalcon\Paginator\Adapter\QueryBuilder(["builder" => $_ads, "limit" => 4, "page" => 1]);
我使用结果集作为数据:
$resultset = $_ads->getQuery()->execute();
$paginator = new Phalcon\Paginator\Adapter\Model(["data" => $resultset, "limit" => 4, "page" => 1]);
经过进一步研究,使用group by
,QueryBuilder
和Paginator
时似乎存在问题。请参阅此open issue in Phalcon。