-3

I have an SQL query for my database to get the 5 most popular queries in my website.

SELECT query, COUNT( query ) AS cnt
FROM search_queries
GROUP BY query
ORDER BY cnt DESC 
LIMIT 5

This works when I use it in phpmyadmin, and it shows my 5 most used queries. Now, I want to convert it to pure CakePHP format so I can include it in my website (using $this->find()). What is the easiest way to write it?

Thanks!

4

2 回答 2

0

Why do you insist on using find()? You can use query() or fetchAll() method, when you have the query prepared.

Look here: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

Maybe you are not interested in this kind of solution, I am just putting it here in case you don't know you can do it like this.

于 2013-09-27T15:59:23.810 回答
0

If you want tu use 'pure CakePHP' you have to create a Model named Query and a controller named QueriesController

in your controller you can use

$this->Query->virtualFields['cnt'] = 'COUNT(query)';
$mostUsedQueries = $this->Query->find(
    'all', 
     array(
        'fields' => array('query', 'cnt'),
        'group' => 'query', 
        'order' => array('cnt' => 'DESC'),
        'limit' => 5
     )
 );

This will generate exactly the same query you posted

于 2013-09-27T16:05:20.843 回答