有人可以給我一個提示,我在做什麼錯?使用SUM ZF2自定義數據庫查詢
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'」?