简体   繁体   English

为什么CakePHP在MySQL查询中使用此方法名称而不返回结果?

[英]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.

相关问题 为什么这个MySQL查询只返回1个结果? - Why Does This MySQL Query Return Just 1 Result? 为什么我的mysql查询总是返回空结果? - Why does my mysql query always return an empty result? 为什么使用__set()和__get()方法而不是将属性声明为public - why use __set() & __get() method rather than declare a property as public MySQL查询IFEXIST不返回任何结果 - Mysql query IFEXIST does not return any result 在Cakephp中,如果我使用直接mysql查询而不是使用模型,如何防止sql注入? - In Cakephp, how to prevent sql injection if I use direct mysql queires rather than using models? 为什么当MySQL驱动程序执行查询时,Codeigniters msqli驱动程序返回null作为查询结果? - Why does Codeigniters msqli driver return null as a query result while the mysql driver executes the query just fine? Laravel DB选择语句。 查询返回列名作为结果而不是值 - Laravel DB select statement. Query returns column name as result rather than values CakePHP:返回Model :: find()的SQL,而不是运行它 - CakePHP: Return the SQL for Model::find() rather than running it 使用名称而不是用户名的数据表查询 - Datatable query using name rather than username 使用POST而不是GET保护CakePHP中的方法 - Protecting a method in CakePHP using POST rather than GET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM