Questions about ZendPaginator and table rows
Posted: Sat Feb 27, 2010 1:20 pm
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:
Here's is the controller code:
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)
And here is the view script:
partials/post-min.phtml
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.
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');
}
}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));
}
}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;
}
}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');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>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.