2015-08-08 58 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