Zend Framework 2 入门-TableGateway中使用Sql扩展的两种方法

来源:互联网 发布:java 汉字转utf8编码 编辑:程序博客网 时间:2024/05/21 18:37

第一种:利用TableGateway中的getSql()方法可以获取Sql对象

<?phpnamespace 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:使用匿名函数

<?phpnamespace 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) {        }    }    ....}
0 0
原创粉丝点击