I am starting with Zend Framework 2
, I want to make a routing choice with the role of My user and I must write getRoleByID($id)
,
then How can'I write
" Select 'role' from user where ('id' = $id) "
with Zend\Db\Sql
I am starting with Zend Framework 2
, I want to make a routing choice with the role of My user and I must write getRoleByID($id)
,
then How can'I write
" Select 'role' from user where ('id' = $id) "
with Zend\Db\Sql
Example Using Select:
$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) { /* ... */ }