Cake语法2.4.7和TreeBehavior中的Postgresql支持的SQL语句问题

时间:2014-11-13 15:34:03

标签: php postgresql cakephp tree cakephp-2.4.7

我使用PostgreSQL作为数据库运行CakePHP 2.4.7应用程序。

在包含PGSQL DBO sourceGroup By时,似乎CakePHP或Order BY没有正确格式化Postgre的SQL查询。

我在尝试保存"简单"时遇到此错误通过TreeBehavior充当树的模型。 它来自POST /创建动作。

数据库引发错误

Error: SQLSTATE[42803]: Grouping error: 7 ERROR: column "Emplacement.reference" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...RE 1 = 1 AND "Emplacement"."id" <> 126 ORDER BY "reference... ^

SQL查询:

SELECT MAX("Emplacement"."rght") AS "rght" 
FROM "mpdefault"."emplacements" AS "Emplacement" 
WHERE 1 = 1 AND "Emplacement"."id" <> 126 ORDER BY "reference" ASC LIMIT 1

堆栈追踪:

CORE/Cake/Model/Datasource/DboSource.php line 458 → PDOStatement->execute(array)
CORE/Cake/Model/Datasource/DboSource.php line 424 → DboSource->_execute(string, array)
CORE/Cake/Model/Datasource/DboSource.php line 665 → DboSource->execute(string, array, array)
CORE/Cake/Model/Datasource/DboSource.php line 1077 → DboSource->fetchAll(string, boolean)
CORE/Cake/Model/Model.php line 2903 → DboSource->read(Emplacement, array)
CORE/Cake/Model/Model.php line 2875 → Model->_readDataSource(string, array)
CORE/Cake/Model/Behavior/TreeBehavior.php line 1013 → Model->find(string, array)
CORE/Cake/Model/Behavior/TreeBehavior.php line 932 → TreeBehavior->_getMax(Emplacement, string, string, integer, boolean)
CORE/Cake/Model/Behavior/TreeBehavior.php line 96 → TreeBehavior->_setParent(Emplacement, string, boolean)
[internal function] → TreeBehavior->afterSave(Emplacement, boolean, array)
CORE/Cake/Utility/ObjectCollection.php line 132 → call_user_func_array(array, array)
[internal function] → ObjectCollection->trigger(CakeEvent)
CORE/Cake/Event/CakeEventManager.php line 247 → call_user_func(array, CakeEvent)
CORE/Cake/Model/Model.php line 1861 → CakeEventManager->dispatch(CakeEvent)
APP/Controller/EmplacementsController.php line 87 → Model->save(array)
[internal function] → EmplacementsController->admin_add()
CORE/Cake/Controller/Controller.php line 490 → ReflectionMethod->invokeArgs(EmplacementsController, array)
CORE/Cake/Routing/Dispatcher.php line 185 → Controller->invokeAction(CakeRequest)
CORE/Cake/Routing/Dispatcher.php line 160 → Dispatcher->_invoke(EmplacementsController, CakeRequest, CakeResponse)
APP/webroot/index.php line 108 → Dispatcher->dispatch(CakeRequest, CakeResponse)

由于查询是在我不了解的核心框架中编写的,因此我请求您的帮助以了解并解决此问题。

您认为将我的版本升级到cakephp-2.6会有所帮助吗?

Postgre SQL(常规SQL规范)拒绝此请求(在我的情况下由cake制作):

SELECT MAX("Emplacement"."rght") AS "rght"
FROM "mpdefault"."emplacements" AS "Emplacement" 
WHERE 1 = 1 AND "Emplacement"."id" = 126 
ORDER BY "reference" ASC LIMIT 1 

但似乎mySQL对此很好。

PostgreSQL接受的查询可能是:

SELECT MAX("Emplacement"."rght") AS "rght"
FROM "mpdefault"."emplacements" AS "Emplacement" 
WHERE 1 = 1 AND "Emplacement"."id" = 126 

SELECT MAX("Emplacement"."rght") AS "rght"
FROM "mpdefault"."emplacements" AS "Emplacement" 
WHERE 1 = 1 AND "Emplacement"."id" = 126 
group by "reference"
ORDER BY "reference" ASC LIMIT 1 

但这些查询是无意义的。

为什么CakePHP会添加&#34; ORDER BY&#34;或&#34; ORDER BY&#34;关于MAX() SELECT的条款?

1 个答案:

答案 0 :(得分:0)

好的原因是我有一个属性Emplacement :: $ order定义为&#34;引用ASC&#34;,然后cakePHP在每个SQL查询后添加ORDER BY reference ASC,导致抛出上一个错误。

我在CakePHP github帐户上针对此问题提出了一个新问题:https://github.com/cakephp/cakephp/issues/5155