Oleg Klimenko
Oleg Klimenko

Reputation: 137

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

Upvotes: 0

Views: 698

Answers (2)

Oleg Klimenko
Oleg Klimenko

Reputation: 137

Based on @Juri 's answer I've done it like this:

$result = User::query()
    ->columns('username, COUNT(post.id) as maximum')
    ->innerJoin('Post', 'post.user_id = User.id', 'post')
    ->groupBy('User.id')
    ->orderBy('maximum DESC')
    ->limit(15)
    ->execute();

I don't know if it is right. But anyway it works for me. Thanks for help. p.s. Maybe it will help to someone too:))

Upvotes: 2

Juri
Juri

Reputation: 1367

$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();

Upvotes: 0

Related Questions