使用mysql的Yi2和php生成器(收益率)
我有一张mysql表格“Flats”,上面有这样的行使用mysql的Yi2和php生成器(收益率),mysql,pagination,yii2,generator,yield,Mysql,Pagination,Yii2,Generator,Yield,我有一张mysql表格“Flats”,上面有这样的行 ----------------------- 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-0
-----------------------
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套公寓)将出现在第一个站点的页面上。然后我需要选择下一个信息(下一个3和4单位)
1) -我如何在yii2中使用php生成器(yield),而不是分页和mysql偏移量来实现这一点。
2) -更好的方法是什么-mysql offset或php yield。我认为这种方法没有任何好处。为什么要重新发明轮子,尤其是当您选择使用框架并且它已经提供分页组件时 您可以在文章中阅读有关分页的常见内容。例如:
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();
通过小部件显示指向页面的链接:
还有一些组件的细节
学习并使用框架特性。例如,当将
ActiveDataProvider
与GridView
小部件一起使用时,分页已经内置,因此您只需将其配置为适合您的需要(每页的项目等),而不必关心内部实现的细节(偏移等)。但如果您了解它的工作原理,显然会更好。我甚至不知道如何在Yii2中启动此代码-如何将活动查询和生成器结合起来
use yii\widgets\LinkPager;
echo LinkPager::widget([
'pagination' => $pagination,
]);