douyou9923 2013-04-03 11:03
浏览 38
已采纳

ZF2 ResultSet和计数性能

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(*)'.

  • 写回答

2条回答 默认 最新

  • dongsheng6056 2013-04-17 14:17
    关注

    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(*)')));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?