0

我的项目中有旧版本(1.2)Propel。我想查询:

SELECT city_id, COUNT(*) FROM House GROUP BY city_id

我试过:

  $c = new Criteria;
  $c->addAsColumn('count', 'Count(' . HousePeer::CITY_ID . ')'); 
  $this->results = HousePeer::doSelect($c);  

但这不起作用 - 只返回数据库中的第一条记录,没有计数等。

我也试过:

$con = Propel::getConnection();
$sql = "SELECT city_id, COUNT(*) FROM House GROUP BY city_id";
$stmt = $con->createStatement();
$rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM);
$this->results = HousePeer::populateObjects($rs);

但这让我

Error populating House object [wrapped: Invalid resultset column: 3]

我只想收到与 SQL 表中相同的内容:

city_id | count
1       | 2
2       | 4
3       | 3

ETC

或从 CityPeer 获取名称 City (我在 City 和 House 之间有正确的关系),例如:

city       | count
New York   | 2
Paris      | 4
Washington | 3

但我不能在 Propel 中使用这个查询。

4

1 回答 1

1

在 symfony 的旧代码片段中有一个很好的例子

尝试:

$c = new Criteria;
$c->clearSelectColumns()->addSelectColumn(HousePeer::CITY_ID);
$c->addGroupByColumn(HousePeer::CITY_ID);
$c->addAsColumn('numCity', 'COUNT('.HousePeer::CITY_ID.')');
$c->addJoin(HousePeer::CITY_ID, CityPeer::ID);

$rs = HousePeer::doSelectRS($c);

while ($rs->next())
{
  // etc ...
}
于 2012-07-17T15:20:22.013 回答