2016-07-31 22 views
-1

我有MySQL表「公寓」與這樣的行Yii2和PHP發電機(產率)與MySQL

----------------------- 
id | created | rooms 
----------------------- 
1 | 2016-07-21 | 2 
2 | 2016-07-20 | 1 
3 | 2016-07-20 | 1 
4 | 2016-07-19 | 2 
5 | 2016-07-18 | 2 
6 | 2016-07-18 | 1 
7 | 2016-07-17 | 2 
8 | 2016-07-17 | 1 
----------------------- 

我需要選擇3個單位用2個房間和4個單位與1間按日期排序(DESC )。該選定的信息(3和4個單位)將位於第1個網站的頁面。然後我需要選擇下一個信息(接下來的3和4個單位)。

1) - 我怎麼能在yii2中使用不分頁而不是mysql偏移量,但使用php生成器(yield)。 2) - 什麼是更好的方法 - mysql的偏移或php的收益爲目的。

+0

你是什麼意思? – rt188

+0

我甚至不知道如何在Yii2中啓動此代碼 - 如何將活動查詢和生成器 – rt188

回答

0

我沒有看到這種方法的好處。爲什麼重新發明輪子,特別是當你選擇使用框架並且它已經提供了分頁組件?

你可以閱讀關於Pagination文章共同的分頁。例如:

use yii\data\Pagination; 

// build a DB query to get all articles with status = 1 
$query = Article::find()->where(['status' => 1]); 

// get the total number of articles (but do not fetch the article data yet) 
$count = $query->count(); 

// create a pagination object with the total count 
$pagination = new Pagination(['totalCount' => $count]); 

// limit the query using the pagination and retrieve the articles 
$articles = $query->offset($pagination->offset) 
    ->limit($pagination->limit) 
    ->all(); 

鏈接頁面顯示與yii\widgets\LinkPager部件:

use yii\widgets\LinkPager; 

echo LinkPager::widget([ 
    'pagination' => $pagination, 
]); 

還有一些細節yii\data\Pagination組件。

學習框架功能並使用它。例如,當ActiveDataProviderGridView小部件一起使用時,分頁已經內置,因此您只將它配置爲適合您的需求(每頁項目等),而不關心內部實現(偏移量等)的細節。但是,如果你明白它的工作原理,這顯然會更好。