2016-07-31 87 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小部件一起使用时,分页已经内置,因此您只将它配置为适合您的需求(每页项目等),而不关心内部实现(偏移量等)的细节。但是,如果你明白它的工作原理,这显然会更好。