【发布时间】:2011-11-10 20:34:41
【问题描述】:
我得到所有城市并使用计数功能:
foreach ($cities as $city) {
echo $city->getName() . '|' . CityTable::getInstance()->getCount($city->getId(), a). '|' . CityTable::getInstance()->getCount($city->getId(), b). '|' . CityTable::getInstance()->getCount($city->getId(), c);
}
public function getCount($id, $num)
{
$q = $this->createQuery('u')
->where('city_id = ?', $id)
->andWhere('num = ?', $num)
->execute();
return count($q);
}
这工作正常,但这会产生许多与数据库的连接。 foreach 中的每次迭代调用了 3 次函数 getCount。如果我在城市表中有超过 1000 个城市,那么我对数据库有超过 10000 个查询。 我怎样才能减少这种情况?
【问题讨论】:
标签: php symfony1 doctrine symfony-1.4 doctrine-1.2