Phalcon Model order by item popularity (number of appearances)

旧城冷巷雨未停 提交于 2019-12-13 03:56:02

问题


I'm sure I have done something like this before, but can't find it and google not being helpful.

Using Phalcon model if possible, I want to select the items from a table whose ID appears the most - i.e. 10 most popular items ordered by popularity. Is this possible using Model::find("conditions")? I do I have to use PHQL for this?


回答1:


using model::find

Model::find([
    'columns' => 'id,count(id) as counter',
    'group' => 'id',
    'order' => 'counter DESC'
]);

PHQL:

$this->modelsManager->executeQuery('SELECT count(id) AS counter,id FROM ModelName GROUP BY id ORDER BY counter DESC');



回答2:


find() does have a group clause, but I don't think it's possible to do what you want because you also need to do a count.

Talal's answer is close, but won't work if you want a list of model objects.

Something like this should work:

$Results = $this->modelsManager->executeQuery('SELECT * FROM ModelName GROUP BY id ORDER BY count(id) DESC LIMIT 10');
$Results->setHydrationMode(\Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS);

Setting the hydration mode may not be necessary, as Phalcon may default to that mode based on the fact the query is asking for *.



来源:https://stackoverflow.com/questions/55400461/phalcon-model-order-by-item-popularity-number-of-appearances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!