1

有人可以给我一个提示我做错了什么吗?

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'”?

4

4 回答 4

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();

但是当您使用上述方法进行选择时,您不再需要它了。

于 2013-11-14T10:38:52.623 回答
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;
    }
于 2012-11-29T06:14:33.163 回答
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;
于 2016-07-19T05:45:10.293 回答
0

尝试那样做而不是(int) $type

 intval($type);

  intval($project_id);

并在您的 sql 中将您的变量更改为

   '".$type."' 

  '".$project_id."'
于 2012-11-28T13:13:27.767 回答