0

我有一个以前已经有一个简单的 fetchAll() 方法的类。我从 ZF2 教程中得到它,它使用 ResultSet 和 TableGateway。只是现在我希望它接受一个我可以检查的参数,以便如果传递了客户 ID,它只会返回具有该客户 ID 的行。

这是该方法的样子:

public function fetchAll($CCID = null)
{
    if($CCID == null){
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    } else {
        // Same as above, only where customerId = $CCID
        return $resultSet;
    }
}

我试过这个(添加使用 Zend\Db\Sql\Sql:

    $resultSet = $this->tableGateway->select->where(array('customerId' => $CCID));

这几乎是一个完整的猜测,但我尝试了它,因为它看起来很有意义。在我的模块文件中,TableGateway 工厂如下所示:

     'JobsTableGateway' => function($sm){
          $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
          $resultSetPrototype = new ResultSet();
          $resultSetPrototype->setArrayObjectPrototype(new Job());
          return new TableGateway('runningjobs', $dbAdapter, null, $resultSetPrototype);
      },

有谁知道如何基本上像条件的第一部分一样执行标准的 fetchAll() ,除了“where”部分?或者也许有更好的方法来做到这一点?

4

1 回答 1

4

阅读文档:

http://framework.zend.com/manual/2.0/en/modules/zend.db.table-gateway.html

公共函数选择($where = null);

所以它应该是这样的:

$resultSet = $this->tableGateway->select(array('customerId' => $CCID));
于 2013-05-23T14:02:50.063 回答