PartialLoop 与 Zend_Paginator 对象问题
是否可以在 Zend_Paginator 对象中使用 findParentRow()
方法?我正在尝试一些代码,该代码可以在 fetchAll 从数据库结果集中返回的对象上正常工作,并且工作正常。但对于 Zend_Paginator 对象,它不起作用。
在我的控制器中,我有:
public function downloadedAction()
{
$images = new Model_ApplicationImages();
$paginator = $images->fetchPaginated();
$paginator->setCurrentPageNumber($this->_getParam('page'));
$this->view->paginator = $paginator;
}
在我的模型中,我有:
public function fetchPaginated()
{
$select = $this->select()
->from($this->_name)
->where('status = ?','approved')
->where('downloaded = ?','0');
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$paginator = new Zend_Paginator($adapter);
$paginator->setItemCountPerPage(10);
return $paginator;
}
在我看来,我有:
$this->partialLoop()->setObjectKey('paginator');
echo $this->partialLoop('admin/list-downloaded.phtml', $this->paginator);
在部分中:
$this->paginator->findParentRow('Model_Application')->name
看起来对象键没有被使用,或者没有像部分 var_dump($this ->paginator)
是 NULL
并且从分页器传递的其他值也在那里,但在 $this->key
下而不是 $ this->paginator->key
正如它们应该的那样
Is it possible to use the findParentRow()
method from within a Zend_Paginator Object? I'm trying some code that works fine on an object returned by fetchAll from a DB resultset and works fine. With the Zend_Paginator object it doesnt work though.
In my controller i have:
public function downloadedAction()
{
$images = new Model_ApplicationImages();
$paginator = $images->fetchPaginated();
$paginator->setCurrentPageNumber($this->_getParam('page'));
$this->view->paginator = $paginator;
}
In my model i have:
public function fetchPaginated()
{
$select = $this->select()
->from($this->_name)
->where('status = ?','approved')
->where('downloaded = ?','0');
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$paginator = new Zend_Paginator($adapter);
$paginator->setItemCountPerPage(10);
return $paginator;
}
In my view i have:
$this->partialLoop()->setObjectKey('paginator');
echo $this->partialLoop('admin/list-downloaded.phtml', $this->paginator);
and in the partial:
$this->paginator->findParentRow('Model_Application')->name
It appears though that the object key is not being used or not being set properly as within the partial var_dump($this->paginator)
is NULL
and the other values being passed from the paginator are there but under $this->key
and not $this->paginator->key
as they should be
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PartialLoop
本质上是为数组中的每个元素或传递给它的Traversable
对象运行一个Partial
。因此,当它到达您的部分视图脚本时,您不再使用Paginator
对象,而是使用其分页内容。在部分循环中,
setObjectKey()
在部分级别工作。您可以将Traversable
对象数组(或迭代Traversable
对象的Traversable
对象)传递给部分循环,并在部分each对象中然后可以通过对象键进行访问。但是,您使用的
DbSelect
分页器适配器会为每个页面返回一个由行组成的数组,因此没有任何对象可以放入分页器中
对象键并且它保持未使用状态。您应该改用
DbTableSelect
适配器,它将返回一个行集。如果您需要访问分页器本身,您应该使用部分分页器。这样您就可以使用
setObjectKey()
自行循环分页器中的页面。我建议您在某些内容未按您预期的方式工作时将 Zend Framework 的源代码放在手边。遗憾的是,我通过阅读代码比阅读文档更成功地弄清楚如何使用它。
A
PartialLoop
essentially runs aPartial
for each element in the array orTraversable
object passed to it. So by the time it gets to your partial view script, you're no longer working with thePaginator
object, but with its paginated contents.In a partial loop, the
setObjectKey()
works at the partial level. You can pass an array ofTraversable
objects (or aTraversable
object that iterates overTraversable
objects) to a partial loop and in the partial each object will then be accessible through the object key.The
DbSelect
paginator adapter you're using, however, returns an array of rows for each page, so there isn't any object to be put in thepaginator
object key and it remains unused.You should use the
DbTableSelect
adapter instead, which will return a rowset.If you need access to the Paginator itself you should use a partial instead. That way you can use
setObjectKey()
to loop over the pages in the paginator yourself.I suggest you keep the source code of the Zend Framework handy when something doesn't work the way you expect. Sadly, I've had more success figuring out how to use it by reading through the code than by reading through the documentation.
实际上,您提供的示例代码非常正确,我有非常相似的设置,尽管我使用 Zend_Paginator_Adapter_DbTableSelect 作为适配器。所以,原则上绝对是可能的。
值得检查控制器操作中 fetchPaginated() 返回的内容。
另一件可能值得检查的事情是 $view->paginator 是否不会在视图脚本中被覆盖(由于不同的原因,这是一件坏事)。另外,您实际上可以使用对象键名称,例如在 setObjectKey() 中使用“model”而不是“paginator”。
PS 不确定它是否相关,但我在控制器中设置了对象键:
Actually the sample code you provided is quite correct, I have very similar setup, albeit I used Zend_Paginator_Adapter_DbTableSelect as an adapter. So, it is definitely possible in principle.
It is worth checking what is returned from fetchPaginated(), in your controller action.
The other thing probably worth checking, is whether $view->paginator doesn't get overwritten in your view script (which is a bad thing for separate reasons). Plus, you can actually play with object key name, use "model" for example instead of "paginator" in your setObjectKey().
P.S. Not sure whether it is relevant or not, but I setup the object key in controller:
Zend_Paginator_Adapter_DbSelect
的getItems()
方法返回数组,Zend_Paginator_Adapter_DbTableSelect
行集的getItems()
方法返回数组,只需使用Zend_Paginator_Adapter_DbTableSelect
,而不是Zend_Paginator_Adapter_DbSelect
...The
getItems()
method ofZend_Paginator_Adapter_DbSelect
returns array andgetItems()
method ofZend_Paginator_Adapter_DbTableSelect
rowset, just useZend_Paginator_Adapter_DbTableSelect
, notZend_Paginator_Adapter_DbSelect
...