[英]Why does CakePHP use this method name in a MySQL query rather than return the result?
In applying the answer from a previous question , I tried overriding one of CakePHP's built-in pagination methods: 在应用上一个问题的答案时,我尝试覆盖CakePHP的内置分页方法之一:
function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
if (isset($extra['group'])) {
for ($i=0; $i<count($extra['group']);$i++) {
if (strpos(strtolower($extra['group'][$i]),'having')!==false) unset($extra['group'][$i]);
}
exit();
}
$count = parent::paginateCount($conditions, $recursive, $extra);
return $count;
}
The problem is that this seems to cause Cake to do a MySQL query consisting simply of the name of the method. 问题在于,这似乎导致Cake进行仅由方法名称组成的MySQL查询。 Obviously this doesn't work and throws an error: 显然,这不起作用并引发错误:
SQL Error: 1064: You have an error in your SQL syntax; SQL错误:1064:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'paginateCount' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的'paginateCount'附近使用正确的语法
Since I have debugging enabled, I can see that where there would normally be the paginateCount() query: 由于启用了调试功能,因此可以看到通常存在paginateCount()查询的位置:
1: SELECT COUNT(*) AS `count` FROM `tournaments` AS `Tournament` WHERE 1 = 1
I just get: 我得到:
1: paginateCount
I've obviously misunderstood PHP's inheritance operators. 我显然误解了PHP的继承运算符。 My function seems to be returning the name of the function it is overriding rather than it's value. 我的函数似乎正在返回被覆盖的函数的名称,而不是其值。
How can I fix this? 我怎样才能解决这个问题?
This usually happens when CakePHP can't find the method in the model or attached behaviors. 当CakePHP在模型或附加行为中找不到方法时,通常会发生这种情况。
It then passes the call to the datasource (hoping it has the method and knows what to do) and that's why you get this error. 然后,它将调用传递给数据源(希望它具有方法并知道该怎么做),这就是为什么会出现此错误的原因。
I would check things are what they seem (is the method in the right model, is Cake making an automodel because of incorrect filenaming, etc.) 我会检查事物的外观(是正确模型中的方法,是因为文件命名不正确,Cake制作了自动模型等)
I've fixed this without really addressing the question I asked. 我已修复此问题,但并未真正解决我提出的问题。
Instead of passing the adjusted parameters up the chain as I wanted to do in my above example, I've used them to do the same job directly: 而不是像上面的示例那样将调整后的参数向上传递,而是使用它们直接完成相同的工作:
return $this->find('count', compact($conditions, $recursive, $extra));
This works OK, but doesn't explain the odd behaviour that caused me to ask the question in the first place. 这可以正常工作,但不能解释导致我首先提出问题的奇怪行为。 My guess is that Cake is doing something funky with virtual/"automagic" methods. 我的猜测是Cake通过虚拟/“自动”方法正在做一些时髦的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.