Reputation: 1548
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(*)'.
Upvotes: 2
Views: 3763
Reputation: 1
What worked for me:
$select->columns(array( 'id'=>'id', 'nr'=>new \Zend\Db\Sql\Expression('COUNT(*)')));
Upvotes: 0
Reputation: 66
Even though you want just a count value, if include the primary key in the columns array, it should work.
$select->columns(array($primkey, 'num' => new \Zend\Db\Sql\Expression('COUNT(*)')));
Upvotes: 3