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

私はこれが古い質問であることを知っていますが、私はそれに出くわし、私は2セントを投入すると考えました:

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 に答える
0

代わりにそのようにしてみてください(int) $type

 intval($type);

  intval($project_id);

そして、SQLで変数を次のように変更します

   '".$type."' 

  '".$project_id."'
于 2012-11-28T13:13:27.767 に答える