开发者

How to get rows count from table in doctrine

I have wrote universal script to get all rows from Doctrine Table Models, but if rows amount is too large, i get exception:

Cannot define NULL as part of query when defining 'offset'.

Running script:

$table = new JV_Model_StoreOrder();
$this->data['lis开发者_Python百科t'] = $table->getTable()->findAll()->toArray();

I understand from the error above is due to the large number of entries in the table (> 20 000). So I decided to make a paginator to break records on the pages of 100 pieces.

Could you help me, how can I do something like that:

...
$total_amount = $table->getTable()->count();
$this->data['list'] = $table->getTable()->offset(0)->limit(100)
->findAll()->toArray();
...


Doctrine has it's own Pagination component:

  • http://www.doctrine-project.org/documentation/manual/1_0/ru/utilities:pagination

Example from Doctrine manual:

// Defining initial variables
$currentPage = 1;
$resultsPerPage = 50;

// Creating pager object
$pager = new Doctrine_Pager(
      Doctrine_Query::create()
            ->from( 'User u' )
            ->leftJoin( 'u.Group g' )
            ->orderby( 'u.username ASC' ),
      $currentPage, // Current page of request
      $resultsPerPage // (Optional) Number of results per page. Default is 25
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜