So I have a query I am trying to Yii-ify.
SELECT links.*, (SUM(votes.karma_delta) - 1) AS karma
FROM links, votes
WHERE links.id = votes.link_id
GROUP BY votes.link_id
ORDER BY (SUM(votes.karma_delta) - 1) / POW((TIMESTAMPDIFF(HOUR, links.created, NOW()) + 2), 1.5) DESC
LIMIT 0, 100
My Link model has it’s proper relations, ie:
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
'votes' => array(self::HAS_MANY, 'Vote', 'link_id'),
But I do not know how to access and/or write the SELECT section, specifically the karma
which is just the sum for my votes.karma_delta
.
When I run my code thus far:
$criteria = new CDbCriteria;
$criteria->select = 'links.*, (SUM(votes.karma_delta)) AS karma';
$criteria->condition = 'links.id = votes.link_id';
$criteria->group = 'votes.link_id';
$criteria->order ='(SUM(votes.karma_delta) - 1) / POW((TIMESTAMPDIFF(HOUR, links.created, NOW()) + 2), 1.5) DESC';
$links = Link::model()->with('votes')->findAll($criteria);
I am greeted with a Yii error. a CDbException
Active record "Link" is trying to select an invalid column "links.*". Note, the column must exist in the table or be an expression with alias.
What am I doing wrong?