phpmysqldatabasephalconphalcon-orm

Get user with maximum count of posts Phalcon


I am newbie in PHP and web framework Phalcon. I've tried a lot but don't find the answer. I try to use its ORM, but don't understand how to generate query.

This is my query in SQL:

SELECT username, count(*) maximum FROM user
    INNER JOIN post ON post.user_id = user.id
GROUP BY user.id
ORDER BY maximum DESC
LIMIT 15

Please help to generate query using Phalcon ORM. Thanks for any replies:)


Solution

  • $result = $modelsManager->createBuilder()
    ->columns('username,COUNT(post.id) as maximum')
    ->from(['user' => '<user class here>'])
    ->innerJoin('<post class here>', 'post.userId = user.id', 'post')
    ->groupBy('user.id')
    ->orderBy('maximum DESC')
    ->limit(15)
    ->getQuery()
    ->execute();