当查询具有“不同”字段时,Zend Paginator 显示错误的页数。

发布于 2024-09-26 14:35:13 字数 1381 浏览 2 评论 0原文

我在一个项目中使用 Zf 1.10,并且在这之前一直非常成功地使用 Zend_Paginator 和 Zend_DbTable 查询。

该查询需要 DISTINCT 关键字来删除由联接创建的重复行,但是当我添加它时,分页器错误地显示两页结果的导航,而实际上只有一页结果。一些挖掘表明它正在执行两个查询,一个用于我想要的结果集(77 行),另一个用于获取计数。但 Zend magic 生成的第二个查询不包含 DISTINCT 关键字,因此计数返回 112 行而不是 77 行。

这是相关的位

$select = $this->select()
    ->setIntegrityCheck(false)
    ->from('companies')
    ->distinct()
    ->join('project_team', 'companies.companyID = project_team.companyID', null)
    ->join('project_team_roles', 'project_team.roleID = project_team_roles.roleID', null)
    ->join('projects', 'projects.projectID = project_team.projectID', null)
    ->where('project_team_roles.isArchitect')
    ->where('companies.companyName LIKE ?', '%' . $str . '%')
    ->where('projects.islive AND NOT projects.isDeleted')
    ->order('companies.companyName'); 
 $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
 $paginator = new Zend_Paginator($adapter);
 $paginator->setCurrentPageNumber($page);
 $paginator->setItemCountPerPage(100);
 return $paginator;

除了忽略计数查询中的 DISTINCT 子句之外,它生成的查询没有任何问题。如果删除 ->distinct() 位,一切都会很好 - 112 行并且分页都是超级的,除了数据有重复的行。

我看到错误报告可以追溯到类似问题,但它们在早期版本的 ZF 中被标记为已修复

?这是一个已知的问题吗?在不编写自己的分页的情况下,我能做些什么吗?这并不是说编写分页特别具有挑战性,但这意味着这一点将与项目的其余部分不一致

非常感谢

Ian

编辑 - 找到了一个解决方法,作为答案发布在下面。

I'm using Zf 1.10 on a project, and have been using Zend_Paginator together with Zend_DbTable queries quite successfully until this bit.

The query requires a DISTINCT keyword to remove duplicate rows created by a join, but when I add it, the paginator incorrectly displays navigation for two pages of results when there is only in fact one page of results. Some digging around reveals that it's executing two queries, one for the result set I'm after (77 rows) and another to get the count. But the second query generated by the Zend magic doesn't include the DISTINCT keyword and so the count returns 112 rows instead of 77 rows.

Here's the relevant bit

$select = $this->select()
    ->setIntegrityCheck(false)
    ->from('companies')
    ->distinct()
    ->join('project_team', 'companies.companyID = project_team.companyID', null)
    ->join('project_team_roles', 'project_team.roleID = project_team_roles.roleID', null)
    ->join('projects', 'projects.projectID = project_team.projectID', null)
    ->where('project_team_roles.isArchitect')
    ->where('companies.companyName LIKE ?', '%' . $str . '%')
    ->where('projects.islive AND NOT projects.isDeleted')
    ->order('companies.companyName'); 
 $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
 $paginator = new Zend_Paginator($adapter);
 $paginator->setCurrentPageNumber($page);
 $paginator->setItemCountPerPage(100);
 return $paginator;

There's nothing wrong with the queries it's generating, other than it's ignoring the DISTINCT clause in the count query. If you remove the ->distinct() bit it all works out great - 112 rows and pagination is all super, except the data has duplicate rows.

I see bug reports going back a fair ways about similar issues but they're marked as fixed in earlier versions of ZF

?Is this is a known gotcha? Is there anything I can do about it without writing my own pagination. It's not that writing pagination is especially challenging, but it means this bit will be inconsistent from the rest of the project

Many thanks

Ian

EDIT - Found a workaround, posted as answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-10-03 14:35:13

这个问题很久以前就被问到了,但仍然需要答案。

您应该在控制器中使用 setRowCount() 方法。

$select = $your_model->getSelect();
$select_count = $your_model->getCount(); // geting count from select above
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$adapter->setRowCount((int)trim($select_count)); // <-- // set integer !
$paginator = new Zend_Paginator($adapter);
// That's it
...

$this->view->paginator = $paginator; 
// and so on...

This was asked a long time ago,but it still needs an answer.

You should use setRowCount() methods in your controller.

$select = $your_model->getSelect();
$select_count = $your_model->getCount(); // geting count from select above
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$adapter->setRowCount((int)trim($select_count)); // <-- // set integer !
$paginator = new Zend_Paginator($adapter);
// That's it
...

$this->view->paginator = $paginator; 
// and so on...
鼻尖触碰 2024-10-03 14:35:13

找到了一个简单的解决方法...不要使用 DbTable 适配器,而是将结果放入数组中,然后将其传递给分页器

$results = $this->fetchAll($select)->toArray(); 
$adapter = new Zend_Paginator_Adapter_Array($results);

看来只有 DbTable 存在此错误。

祝你好运!

Found a simple workaround... Don't use the DbTable adapter, but get the results into an array and then pass that to the paginator

$results = $this->fetchAll($select)->toArray(); 
$adapter = new Zend_Paginator_Adapter_Array($results);

It's only the DbTable that has this bug, it would seem.

Best of luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文