0

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Loouu
  • 19
  • 7

1 Answers1

0

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) { /* ... */ }
Andrew
  • 12,617
  • 1
  • 34
  • 48
  • thank you for your reponce , it's very helpful . Just one question , i must execute the query with // $stmt = $this->getSql()->prepareStatementForSqlObject($select); $results = $stmt->execute(); // to can compare with the result ?? – Loouu Jun 28 '13 at 14:04