Questions about ZendPaginator and table rows

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Questions about ZendPaginator and table rows

Post by Darhazer »

Hi,

I'm adding a paging to existing code.

I need to make some data available in the partial viewscript, used by the paginator class.
The problem is that I cannot join the table, that contain the data I need (because of the db design), so I'm getting the items from the paginator and I'm adding the new variable. And here's the second problem, Zend_Db_Table_Row does not allow setting of data for a column that does not exist. So I've wrote a workaround, but It's workaround and I'm wondering how can I handle this properly.

But first, code to illustrate:

Code: Select all

 
class Blog_Post {
    /**
    * this is a function I've wrote
    * this->select() returns the Zend_Db_Table_Select object
    **/
    public function getPostsRecordset($blogId)
    {
        return $this->select()->where('parent_id = ?', $blogId)->order('create_date', 'desc');
    }
}
Here's is the controller code:

Code: Select all

$mdlBlog = new Blog_Blog();
            $mdlPost = new Blog_Post();
 
                $this->view->blog = $mdlBlog->find($this->moduleData->blog)->current();
                $posts = $mdlPost->getPostsRecordset($this->moduleData->blog);
                $this->view->paginator = Zend_Paginator::factory($posts);
                
                $this->view->paginator->setCurrentPageNumber($page);
                $this->view->paginator->setDefaultItemCountPerPage(1);
                // load teasers
                $items = $this->view->paginator->getCurrentItems();
                foreach ($items as $item) {
                    $decorator = new RowDecorator($item); // the workaround part
                    $decorator->set('content', $mdlPost->openPost($item->id));
                }
            }
Here is the RowDecorator (it's not a decorator as defined by the purpose of this patterns, but since the implementation is decorator-like, I prefer to call such classes decorators)

Code: Select all

class RowDecorator extends Zend_Db_Table_Row {
    protected $_row;
    public function __construct(Zend_Db_Table_Row $row)
    {
        $this->_row = $row;
    }
    
    public function set($key, $value)
    {
        $this->_row->_data[$key] = $value;
    }
}
And here is the view script:

Code: Select all

<h2 class='blog-title'><?php echo $this->blog->name; ?></h2><?php
    
    echo $this->partialLoop('partials/post-min.phtml', $this->paginator);
    echo $this->paginationControl($this->paginator, 'Sliding', 'partials/search-pagination.phtml');
partials/post-min.phtml

Code: Select all

<div class="blog_post">
<?phpod
$link = Digitalus_Uri::get(false, false, array('openPost' => $this->id));
?>
<a href="<?php echo $link;?>">
<h3 class="blog_post_title"><?php echo $this->name ?></h3>
</a>
<p><?php echo $this->content->teaser ?></p>
</div>
It's all about the $this->content->teaser, which is retrieved by the BlogPost :: openPost() method

So, any ideas how this can be accomplished without the 'decorator'? Some kind of adapter? So far I know that I can perform fetchAll and to use paginator on the array, but I do not want to fetch everything from the database, this is the main reason I'm writting the paginator. Any suggestion will be appreciated.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Questions about ZendPaginator and table rows

Post by Eran »

Count and paginate manually. Not everything has to use ZF provided components - especially if they don't fit the scenario well.
I mostly use my own paginator instead of zend's
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Questions about ZendPaginator and table rows

Post by Darhazer »

Thanks pytrin, I'll do it this way.
Getting the required subset of rows from the model, and the total count is really easily, as well as passing the page param around. Is there easy way to render the navigation, some ready-to-use view helper?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Questions about ZendPaginator and table rows

Post by Eran »

Not that I'm aware of. I took the process phpBB uses to create their pagination almost as is, just cleaned it up a bit and made a helper out of it. It's somewhat of kludge, but I don't think there's an elegant solution to all those presentation conditionals. I've attached my version to this post
Attachments
Pagination.zip
(849 Bytes) Downloaded 559 times
Post Reply