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条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化