Zend / db / Sql /查询语法

时间:2013-06-28 10:42:25

标签: sql zend-framework2 zend-db

我从Zend Framework 2开始,我想使用我的用户角色做出路由选择,我必须写getRoleByID($id)

然后 怎么可以'我写'

使用Zend \ Db \ Sql

" Select 'role' from user where ('id' = $id) "

1 个答案:

答案 0 :(得分:0)

使用选择的示例:

$select = new \Zend\Db\Sql\Select('user');
$select->columns(array('role'));
$where = new Where();
$where->equalTo('id', $id);
$select->where($where);
/**
 * Simple example of executing a query...
 */
$stmt = $this->getSql()->prepareStatementForSqlObject($select);
$results = $stmt->execute();
/* @var $results \Zend\Db\Adapter\Driver\Pdo\Result */

if( ! $results->count()) {
     // do something, none found...
}

$row = $results->current(); 
return $row['role'];

// if you had multiple results to iterate over:
//$resultSet = new \Zend\Db\ResultSet\ResultSet();
//$resultSet->initialize($results);    
//$array = $resultSet->toArray();
//foreach($resultSet as $row) { /* ... */ }