Zend Framework Source Code Snippets

Paginator_Adapter_DoctrineQuery

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105106
107
108
<?php
/**
 * SM's code library
 * 
 *  * @category    Smapp     
 * @package     Smapp_Paginator
 * @copyright   Copyright (c) 2009 Pavel V Egorov
 * @author      Pavel V Egorov
 * @link        http://epavel.ru/ * @since       14.08.2009
 */
 
 
/** * @see Zend_Paginator_Adapter_Interface
 */
require_once 'Zend/Paginator/Adapter/Interface.php';
 
/** * @category    Smapp     
 * @package     Smapp_Paginator
 * @copyright   Copyright (c) 2009 Pavel V Egorov
 * @author      Pavel V Egorov
 * @link        http://epavel.ru/ * @since       14.08.2009
 */
class Smapp_Paginator_Adapter_DoctrineQuery implements Zend_Paginator_Adapter_Interface
{
    /**     * Database query
     *
     * @var Doctrine_Query
     */
    protected $_query = null; 
    /**
     * Total item count
     *
     * @var integer     */
    protected $_rowCount = null;
 
    /**
     * Constructor.     *
     * @param Doctrine_Query $q The select query
     */
    public function __construct(Doctrine_Query $q)
    {        $this->_query = $q;
    }
 
    /**
     * Sets the total row count     *
     * @param  Doctrine_Query|integer $totalRowCount Total row count integer
     *                                               or query
     * @return Zend_Paginator_Adapter_DbSelect $this
     * @throws Zend_Paginator_Exception     */
    public function setRowCount($rowCount)
    {
        if ($rowCount instanceof Doctrine_Query) {
            $this->_rowCount = $rowCount->count();        } else if (is_integer($rowCount)) {
            $this->_rowCount = $rowCount;
        } else {
            /**
             * @see Zend_Paginator_Exception             */
            require_once 'Zend/Paginator/Exception.php';
 
            throw new Zend_Paginator_Exception('Invalid row count');
        } 
        return $this;
    }
 
    /**     * Returns an array of items for a page.
     *
     * @param  integer $offset Page offset
     * @param  integer $itemCountPerPage Number of items per page
     * @return array     */
    public function getItems($offset, $itemCountPerPage)
    {
        return $this->_query->limit($itemCountPerPage)
                            ->offset($offset)                            ->execute();
    }
 
    /**
     * Returns the total number of rows in the result set.     *
     * @return integer
     */
    public function count()
    {        if ($this->_rowCount === null) {
            $this->setRowCount(
                $this->_query->count()
            );
        }        return $this->_rowCount;
    }
}

Comments

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Usage:

$q = Doctrine_Query::create()
->from('Movie m');

$paginator = new Zend_Paginator(new Smapp_Paginator_Adapter_DoctrineQuery($q));

And use it as usual Zend paginator:

$dataset = $paginator->setItemCountPerPage(48)
->setCurrentPageNumber($this->_getParam('page', 1));

$this->view->paginator = $paginator;

Snippet details

Created:
SM SM
1 year ago
Edited:
SM SM
1 year ago
Revision Id:
123
Edit Message:
Initial Release
ZF Version
1.8.3
Tags:
zend_paginator doctrine
Comments:
0
Views:
807
Points:
1 (1 votes)

History

r123

Initial Release

SM SM
1 year ago
diff
r122

Initial Release

SM SM
1 year ago