2012-11-28 52 views
1

有人可以給我一個提示,我在做什麼錯?使用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'」?

回答

0

嘗試做出那樣的,而不是(int) $type

intval($type); 

intval($project_id); 

,並在SQL 改變你的變量

'".$type."' 

'".$project_id."' 
2

好吧,現在這是工作,告訴我這是應該怎麼做?

public function getPaymentSumByTypeAndProject($project_id, $type) { 
     $type = (int) $type; 
     $project_id = (int) $project_id; 
     $adapter = $this->tableGateway->adapter; 
     $sql = new Sql($adapter); 
     $select = $sql->select(); 
     $select->from('payment'); 
     $select->where(array('type'=>$type,'project_id'=>$project_id)); 
     $select->columns(array(new \Zend\Db\Sql\Expression('SUM(amount) as amount'))); 
     $selectString = $sql->getSqlStringForSqlObject($select); 
     $resultSet = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE); 
     return $resultSet; 
    } 
4

我知道這是一個老問題,但我碰到它,想我會在我的兩分錢拋出:

public function getPaymentSumByTypeAndProject($project_id, $type) { 
    // This TableGateway is already setup for the table 'payment' 
    // So we can skip the ->from('payment') 
    $sql = $this->tableGateway->getSql(); 

    // We'll follow the regular order of SQL (SELECT, FROM, WHERE) 
    // So the query is easier to understand 
    $select = $sql->select() 
      // Use an alias as key in the columns array instead of 
      // in the expression itself 
      ->columns(array('amount' => new \Zend\Db\Sql\Expression('SUM(amount)'))) 
      // Type casting the variables as integer can take place 
      // here (it even tells us a little about the table structure) 
      ->where(array('type' => (int)$type, 'project_id' => (int)$project_id)); 

    // Use selectWith as a shortcut to get a resultSet for the above select 
    return $this->tableGateway->selectWith($select); 
} 

此外,適配器可以像表網關檢索這個:

$adapter = $this->tableGateway->getAdapter(); 

但是當你選擇使用上面提到的方法時,你不再需要它了。

1

使用這一個

$select = $this->getSql()->select() 
     ->columns(array('amount' => new \Zend\Db\Sql\Expression('SUM(amount)'))) 
     ->where("type ='$type'") 
     ->where("project_id ='$project_id'"); 
    $resultSet = $this->selectWith($select); 
    $row = $resultSet->current(); 
    // echo $select->getSqlString();die; //check query use this line 
    if(!$row){ 
     return False; 
    } 
    return $row->amount;