我如何在 zf2 tablegateway 中绑定参数

How can i bind parameter in zf2 tablegateway

我如何使用 tablegateway 在 zend framework 2 中绑定参数,这是我使用的代码

$adapter     =   $this->tableGateway->getAdapter(); 
$result      =   $adapter->query(
              "SELECT * "
             . "FROM TABLE "
             . "WHERE SOME_ID = $SOME "
             . "AND STATUS = 1 "          
);
$dataSource  =   $result->execute();        
$statement   =   $dataSource->getResource();
$result      =   $statement->fetchAll(\PDO::FETCH_OBJ);

请给我建议一个安全的查询生成器代码

您正在尝试在 Adapter 中而不是在 TableGateway 中绑定参数。
可以通过多种方式完成,但例如 post

$id  = 123;
$res = $adapter->query(
    "SELECT * FROM TABLE WHERE SOME_ID = ? AND STATUS = 1", [$id]
);
var_dump($res->current());

函数 query() 中有第二个参数

@param string|array|ParameterContainer $parametersOrQueryMode 

所以你可以用这个选项玩一点...还要检查函数 Zend\Db\Adapter\Adapter::query();


更简单的方法是使用 TableGateway:

$res = $this->tableGateway->select(['SOME_ID' => $id]);
$res->current(); // than you can use also toArray(), current(), etc.