有人可以给我一个提示我做错了什么吗?
public function getPaymentSumByTypeAndProject($project_id,$type) {
$type = (int) $type;
$project_id = (int) $project_id;
$rowset = $this->tableGateway->select(array('total_amount' => new Expression('SUM(payment.amount)')))->where(array('type' => $type, 'project_id' => $project_id));
$row = $rowset->toArray();
if (!$row) {
throw new \Exception("Busted :/");
}
return $rowset;
}
我想做同样的查询:
SELECT SUM(amount) FROM payment WHERE type='$type' AND project_id ='$project_id';
编辑:我取得了很小的进步,我已经想出了如何对整列求和
public function getPaymentSumByTypeAndProject($project_id, $type) {
$type = (int) $type;
$project_id = (int) $project_id;
$resultSet = $this->tableGateway->select(function (Select $select) {
$select->columns(array(new \Zend\Db\Sql\Expression('SUM(amount) as amount')))->where('type="0"');
});
return $resultSet;
也许有人可以帮我弄清楚如何添加条件:“WHERE type='$type' AND project_id='$project_id'”?