Why does CakePHP use this method name in a MySQL q

2020-04-19 07:43发布

问题:

In applying the answer from a previous question, I tried overriding one of CakePHP's built-in pagination methods:

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. Obviously this doesn't work and throws an error:

SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'paginateCount' at line 1

Since I have debugging enabled, I can see that where there would normally be the paginateCount() query:

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. My function seems to be returning the name of the function it is overriding rather than it's value.

How can I fix this?

回答1:

This usually happens when CakePHP can't find the method in the model or attached behaviors.

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.)



回答2:

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.