2015-08-08 100 views
1

使用PAGINATE据商务部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));}

所以应该对结果集工作。或者我不明白文档解释的内容。可能。

回答

3

这意味着你正在传递错误类型的对象。分页结果集不支持,只有表(对象或名称)和查询。

groupBy不是查询类的方法,它是导致查询被执行的魔术方法之一,并将方法调用转发给结果集。所以你最终打电话Cake\ORM\ResultSet::groupBy(),它返回另一个集合。

所以如果你需要在分页这样的分组结果,那么你必须通过获取对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' 
      ]); 
    }); 

你当然必须相应地调整你的视图代码。

+0

看来你是对的,无论如何doc似乎说结果集被支持。看到我的编辑。 – 2ndGAB

+0

@caBBAlainB Nope,文档说你可以传递查询。 “_resulting query_”不是“_result set_”,这是两个不同的东西,两个不同的类('\ Cake \ ORM \ Query','\ Cake \ ORM \ ResultSet'),前者是一个正在返回的查询结果,后者是执行查询时返回的结果(这就是你正在做的)。 ** http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#working-with-result-sets**,** http://book.cakephp.org/ 3.0/en/orm/query-builder.html#queries-are-collection-objects ** – ndm

+0

好的,好吧,我明白了。谢谢。 – 2ndGAB