简体   繁体   English

带有Zend_Paginator对象的PartialLoop问题

[英]PartialLoop with Zend_Paginator Object Issue

Is it possible to use the findParentRow() method from within a Zend_Paginator Object? 是否可以在Zend_Paginator对象中使用findParentRow()方法? I'm trying some code that works fine on an object returned by fetchAll from a DB resultset and works fine. 我正在尝试一些代码,该代码在数据库结果集中由fetchAll返回的对象上运行良好,并且运行良好。 With the Zend_Paginator object it doesnt work though. 但是,使用Zend_Paginator对象无法正常工作。

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 似乎未使用或未正确设置对象键,因为在局部var_dump($this->paginator)NULL并且从paginator传递的其他值在那里但在$this->key而不是$this->paginator->key应该是

A PartialLoop essentially runs a Partial for each element in the array or Traversable object passed to it. PartialLoop本质上为传递给它的数组或Traversable对象中的每个元素运行Partial So by the time it gets to your partial view script, you're no longer working with the Paginator object, but with its paginated contents. 因此,到部分视图脚本时,您将不再使用Paginator对象,而是使用其分页的内容。

In a partial loop, the setObjectKey() works at the partial level. 在部分循环中, setObjectKey()在部分级别上工作。 You can pass an array of Traversable objects (or a Traversable object that iterates over Traversable objects) to a partial loop and in the partial each object will then be accessible through the object key. 您可以将Traversable对象数组(或Traversable对象的Traversable对象)传递给部分循环,然后在部分循环中,每个对象都可以通过对象键进行访问。

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 the paginator object key and it remains unused. 但是,您正在使用的DbSelect分页器适配器为每个页面返回一个行数组 ,因此在paginator对象键中没有要放置的任何对象,并且它保持未使用状态。

You should use the DbTableSelect adapter instead, which will return a rowset. 您应该改用DbTableSelect适配器,它将返回一个行集。

If you need access to the Paginator itself you should use a partial instead. 如果您需要访问Paginator本身,则应改用Partialator。 That way you can use setObjectKey() to loop over the pages in the paginator yourself. 这样,您可以使用setObjectKey()自己遍历分页器中的页面。

I suggest you keep the source code of the Zend Framework handy when something doesn't work the way you expect. 我建议您在某些无法按您期望的方式工作时,将Zend Framework的源代码放在手边。 Sadly, I've had more success figuring out how to use it by reading through the code than by reading through the documentation. 可悲的是,通过阅读代码比阅读文档更能成功地弄清楚如何使用它。

Actually the sample code you provided is quite correct, I have very similar setup, albeit I used Zend_Paginator_Adapter_DbTableSelect as an adapter. 实际上,您提供的示例代码是非常正确的,尽管我使用Zend_Paginator_Adapter_DbTableSelect作为适配器,但设置却非常相似。 So, it is definitely possible in principle. 因此,原则上绝对有可能。

It is worth checking what is returned from fetchPaginated(), in your controller action. 值得在控制器操作中检查从fetchPaginated()返回的内容。

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). 可能值得检查的另一件事是$ view-> paginator是否不会在您的视图脚本中被覆盖(由于不同的原因,这是一件坏事)。 Plus, you can actually play with object key name, use "model" for example instead of "paginator" in your setObjectKey(). 另外,您实际上可以使用对象键名来玩,例如在setObjectKey()中使用“模型”代替“ paginator”。

PS Not sure whether it is relevant or not, but I setup the object key in controller: PS不确定是否相关,但是我在控制器中设置了对象密钥:

$this->view->partialLoop()->setObjectKey('model');
$this->view->partial()->setObjectKey('model');

Zend_Paginator_Adapter_DbSelectgetItems()方法返回Zend_Paginator_Adapter_DbTableSelect行集的数组和getItems()方法,仅使用Zend_Paginator_Adapter_DbTableSelect ,而不是Zend_Paginator_Adapter_DbSelect ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM