I'm trying to execute SQL query with table gateway which contain COUNT(*) expression in ZF2. This is function in my model:
public function brKomentariUred(){
$sql = $this->tableGateway->getSql();
$select = $sql->select();
$select->columns(array('brKomentari' => new \Zend\Db\Sql\Expression('count(komentarID)'), 'uredId' => 'ured'));
$select->group('ured');
//echo $sql->getSqlStringForSqlObject($select); die();
return $this->tableGateway->selectWith($select);
}
When the query is printed it is correct
SELECT count(komentarID) AS `brKomentari`, `komentar`.`ured` AS `uredId` FROM `komentar` GROUP BY `ured`
In the controller I'm trying to call the query with this code
foreach($this->getKomentarTable()->brKomentariUred() as $r){
$arr = $this->object_to_array($r);
print_r($arr);
}
It doesn't return number of elements and devicesID as it is written in SELECT, but return as SELECT * FROM komentar
, but with no values. Is this right code or I'm making some error in my code? Other queries are OK.
Thanks in advance for your help.
devicesID
column in your query – RomanPerekhrest