不能对查询结果使用分页
Can't use paginate on a query result
根据文档 http://book.cakephp.org/3.0/en/controllers/components/pagination.html,我想对如下查询的结果进行分页:
$unlocked_sitesQuery = $this->Deviceconnections
->find()
->contain([
'Agthemes.Sites',
'Agthemes.Agpois',
'Agthemes.Agthemelanguages'
])
->where(['request' => 'unlock'])
->groupBy('agtheme.site.id');
$unlocked_sites = $this->paginate($unlocked_sitesQuery);
但我收到以下错误:
Error: Call to undefined method ArrayIterator::alias()
File /home/mywebsite/vendor/cakephp/cakephp/src/Controller/Component/PaginatorComponent.php
Line: 154
这是什么意思?
编辑
看起来 @ndm 是对的,但文档说:
By default the paginate()
method will use the default model for a controller. You can also pass the resulting query of a find method:
public function index()
{
$query = $this->Articles->find('popular')->where(['author_id' => 1]);
$this->set('articles', $this->paginate($query));
}
所以它应该适用于结果集。或者我不明白文档解释了什么。可能。
这意味着您传递了错误类型的对象。不支持对结果集进行分页,仅支持表(对象或名称)和查询。
groupBy
不是查询 class 的方法,它是导致查询执行的神奇方法之一,并将方法调用转发到结果集。所以你最终调用了 Cake\ORM\ResultSet::groupBy()
,returns 另一个集合。
- Cookbook > ... ORM > Query Builder > Queries Are Collection Objects
- Cookbook > ... ORM > Retrieving Data & Results Sets > Working with Result Sets
因此,如果您在分页中需要这样的分组结果,那么您必须(至少部分)在 SQL 级别上解决这个问题,例如通过相反的方式获取结果,即获取 Sites
和它们的关联,并按 Deviceconnections.request
过滤,像这样(不能保证这会给你想要的结果,但这个例子应该给你一个提示!):
$query = $Sites
->find()
->contain([
'Agthemes.Deviceconnections',
'Agthemes.Agpois',
'Agthemes.Agthemelanguages'
])
->matching('Agthemes.Deviceconnections', function(\Cake\ORM\Query $query) {
return $query
->where([
'Deviceconnections.request' => 'unlock'
]);
});
您当然必须相应地调整您的视图代码。
根据文档 http://book.cakephp.org/3.0/en/controllers/components/pagination.html,我想对如下查询的结果进行分页:
$unlocked_sitesQuery = $this->Deviceconnections
->find()
->contain([
'Agthemes.Sites',
'Agthemes.Agpois',
'Agthemes.Agthemelanguages'
])
->where(['request' => 'unlock'])
->groupBy('agtheme.site.id');
$unlocked_sites = $this->paginate($unlocked_sitesQuery);
但我收到以下错误:
Error: Call to undefined method
ArrayIterator::alias()
File /home/mywebsite/vendor/cakephp/cakephp/src/Controller/Component/PaginatorComponent.php
Line: 154
这是什么意思?
编辑 看起来 @ndm 是对的,但文档说:
By default the
paginate()
method will use the default model for a controller. You can also pass the resulting query of a find method:
public function index()
{
$query = $this->Articles->find('popular')->where(['author_id' => 1]);
$this->set('articles', $this->paginate($query));
}
所以它应该适用于结果集。或者我不明白文档解释了什么。可能。
这意味着您传递了错误类型的对象。不支持对结果集进行分页,仅支持表(对象或名称)和查询。
groupBy
不是查询 class 的方法,它是导致查询执行的神奇方法之一,并将方法调用转发到结果集。所以你最终调用了 Cake\ORM\ResultSet::groupBy()
,returns 另一个集合。
- Cookbook > ... ORM > Query Builder > Queries Are Collection Objects
- Cookbook > ... ORM > Retrieving Data & Results Sets > Working with Result Sets
因此,如果您在分页中需要这样的分组结果,那么您必须(至少部分)在 SQL 级别上解决这个问题,例如通过相反的方式获取结果,即获取 Sites
和它们的关联,并按 Deviceconnections.request
过滤,像这样(不能保证这会给你想要的结果,但这个例子应该给你一个提示!):
$query = $Sites
->find()
->contain([
'Agthemes.Deviceconnections',
'Agthemes.Agpois',
'Agthemes.Agthemelanguages'
])
->matching('Agthemes.Deviceconnections', function(\Cake\ORM\Query $query) {
return $query
->where([
'Deviceconnections.request' => 'unlock'
]);
});
您当然必须相应地调整您的视图代码。