Search code examples
symfonysql-order-bysymfony-1.4

Symfony 1.4 Criteria addGroupByColumn then order by most


I use symfony 1.4 and I want to group some data, order it by grouped columns then select the one who has the most records. My code looks like this:

$c = new Criteria();
$c->addGroupByColumn(MetricPeer::POST_TYPE_ID);
$c->addDescendingOrderByColumn(MetricPeer::POST_TYPE_ID);    
$posts = MetricPeer::doSelectOne($c);

I know that line "$c->addDescendingOrderByColumn(MetricPeer::POST_TYPE_ID);" it is ordering data just by the numbers and it is not correct.


Solution

  • public static function topPosts($limit,$source_id,$interval1,$interval2,$con = null) {
        if($con === null) $con = Propel::getConnection(self::DATABASE_NAME);
        $sql = "SELECT *, COUNT(post_id) AS value_occurrence FROM metric WHERE source_id LIKE ".$source_id." AND visit_out BETWEEN '".$interval1."' AND  '".$interval2."' GROUP BY post_id ORDER BY value_occurrence DESC LIMIT ".$limit."";
        $stmt = $con->prepare($sql);
        $stmt->execute();       
        $topreads = MetricPeer::populateObjects($stmt);
        return $topreads;
    }
    

    This is my humble solution for now, I thought it might be useful for others, too. Thank you.