I have two tables:
1- article(id , title, lead, abstract ,…)
2- article_views(id, article_id, ip, …)
relation beetween article.id and article_view.article_id
I need show popular articles,
So, I do this:
in article model class i have:
public function relations()
{
...
'articleViews' => array(self::HAS_MANY,'ArticleViews', 'article_id'),
'viewCount' => array(self::STAT, 'ArticleViews','article_id'),
...
}
...
...
public function getPopularArticles($limit)
{
...
}
in getPopularArticles I try this code:
$articles = $this->with('viewCount')->findAll(array('limit'=>$limit));
So, how can I add order by for viewCount?
Also in think my code is bad(maybe wrong) because in log i see this code:
SELECT `article_id` AS `c`, COUNT(*) AS `s` FROM
`wom_article_views` `t` WHERE (`t`.`article_id` IN (1, 2, 3, 5, 6, 7, 8, 9,
10, 11,
But my query must be:
SELECT wom_article.title, COUNT( wom_article_views.id ) AS c
FROM wom_article
INNER JOIN wom_article_views ON ( wom_article.id = wom_article_views.article_id )
GROUP BY wom_article.id
ORDER BY c DESC
Whats wrong?