I'm using TableGateway with an attached custom RowGateway object. If I want to use the object returned by TableGateway->getSql()->select() to get a record count, the attached RowGateway object complains about a missing primary key in the result set.
$tablegateway = new TableGateway('table', $adapter, new RowGatewayFeature(new AuditingRowGateway($primkey, 'table', $adapter), new ResultSet());
$select = $tablegateway->getSql()->select();
$select->columns(array('num' => new \Zend\Db\Sql\Expression('COUNT(*)')));
$row = $tablegateway->selectWith($select)->current();
Result: Zend\Db\RowGateway\Exception\RuntimeException: While processing primary key data, a known key xxx was not found in the data array
I could work it around by issuing a normal (i.e. non-count) select:
$result = $tablegateway->selectWith($select);
$count = $result->count();
But not sure about this performance-wise compared to a 'SELECT COUNT(*)'.