第一种:利用TableGateway中的getSql()方法可以获取Sql对象
<?php namespace Application\Model; use Zend\Db\TableGateway\TableGateway; class UserTable { private $tableGateway; public function __construct(TableGateway $tableGateway) { $this->tableGateway = $tableGateway; } public function getUser($id) { $sql = $this->tableGateway->getSql(); $select = $sql->select(); $select->columns(array('id', 'user')) ->where(array('id' => $id)); try { return $this->tableGateway->selectWith($select); } catch (\Exception $e) { } } .... }
方法2:使用匿名函数
<?php namespace Application\Model; use Zend\Db\Sql\Select; use Zend\Db\TableGateway\TableGateway; class UserTable { private $tableGateWay; public function __construct(TableGateway $tableGateway) { $this->tableGateway = $tableGateway; } public function getUser($id) { try { return $this->tableGateway->select(function(Select $select){ $select->columns(array('id', 'name')) ->where(array('id' => $id)); }); } catch (\Exception $e) { } } .... }