用Propel计算和分组

Joh*_*erg 7 php propel symfony1

在Doctrine中,我可以做到:

public function getCount() 
{        
        $q = $this->createQuery('q')
            ->select('*')
            ->addSelect('count(q.name) as count')
            ->groupBy('q.name')
            ->orderBy('count DESC');

        return $q->execute();        
}
Run Code Online (Sandbox Code Playgroud)

如何在Symfony 1.4中的Propel中做同样的事情?

Wil*_*and 10

该死的!它比那更容易!

如果需要计算给定查询的结果行,则需要使用count()终止方法,基本上:

MyTableQuery::create()->count();
Run Code Online (Sandbox Code Playgroud)

有关更多信息,阅读以下文档部分:http://www.propelorm.org/documentation/03-basic-crud.html#query_termination_methods

如果要在查询中添加一个count或多个nb列,表示像COUNT或者的SQL聚合函数SUM,那么您应该使用以下withColumn()方法:

$query = MyTableQuery::create()
    ->withColumn('COUNT(*)', 'Count')
    ->select(array('Name', 'Count'))
    ->groupByName()
    ->orderByCount()
    ;

$results = $query->find();
Run Code Online (Sandbox Code Playgroud)


Man*_*eUK 2

尝试 :

public function getCount() 
    $c = new Criteria();
    $c->addAsColumn('count', 'count(name)');
    $c->addDescendingOrderByColumn($c->getColumnForAs('count')); 
    $c->addGroupByColumn('name');
    return self::doCount($c);
}
Run Code Online (Sandbox Code Playgroud)

这里有一些关于 propel 查询的很好的信息片段 -> http://snippets.symfony-project.org/snippets/tagged/criteria/order_by/date