这里的问题是您使用 Category 模型作为入口点,而您似乎应该使用 Game 模型的内置查询方法以及过滤器、限制和偏移量,句号。这将允许您在不需要添加所有这些复杂性来支持您的之前/之后概念的情况下进行分页。这是不必要的。 如果你的关系设置正确,你只需要对游戏进行分页就是一个游戏模型查询!
例如,您似乎需要一个在特定类别中成熟的游戏的分页列表,对吗?要获得符合这些条件的 5 场比赛的结果,您需要发送一个 json REST API 调用,如下所示:
http://0.0.0.0:3000/api/Games?filter=%7B%20%22where%22%3A%20%7B%22mature%22%3Atrue%2C%20%22categoryId%22%3A%201004%7D%2C%20%22offset%22%3A%200%2C%20%22limit%22%3A%205%20%7D
这很难阅读,因为它是编码的,但底层过滤器是这样构造的:
{ "where": {"mature":true, "categoryId": 1004}, "offset": 0, "limit": 5 }
这将返回以下数据集,即前 5 款属于 1004 categoryId 且同样成熟的游戏:
[
{
"mature": true,
"gameName": "CODBlacOps3",
"categoryId": 1004,
"gameId": 1000053,
"description": "Published by Activision",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Evolve",
"categoryId": 1004,
"gameId": 1000054,
"description": "Published by Turtle Rock Studios",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Battlefield4",
"categoryId": 1004,
"gameId": 1000055,
"description": "Published by EA Digital Illusions",
"publishedDate": "2013-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Rainbow6",
"categoryId": 1004,
"gameId": 1000056,
"description": "Published by EUbisoft",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Destiny",
"categoryId": 1004,
"gameId": 1000057,
"description": "Published by Bungie",
"publishedDate": "2014-01-01T00:00:00.000Z"
}
]
要获得 5 个游戏的下一页,我们只需将偏移量更改为 5,这会告诉数据库跳过前 5 个,因为我们现在在第 2 页:
{ "where": {"mature":true, "categoryId": 1004}, "offset": 5, "limit": 5 }
这将返回以下结果:
[
{
"mature": true,
"gameName": "Wolfenstein",
"categoryId": 1004,
"gameId": 1000058,
"description": "Published by Bethesda",
"publishedDate": "2014-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "StarWarsBattleFront",
"categoryId": 1004,
"gameId": 1000059,
"description": "Published by EA DICE",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Test1",
"categoryId": 1004,
"gameId": 1000060,
"description": "Published by Test1",
"publishedDate": "2015-12-19T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Test2",
"categoryId": 1004,
"gameId": 1000061,
"description": "Published by Test2",
"publishedDate": "2015-12-19T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Test3",
"categoryId": 1004,
"gameId": 1000062,
"description": "Published by Test3",
"publishedDate": "2015-12-19T00:00:00.000Z"
}
]
默认情况下,游戏按照其 game_id 的顺序排列。无需自定义远程方法和嵌套的 find()!
如果您想按字母顺序排列列表,还可以添加order 过滤器,并将其设置为"order": "gameName ASC":
{ "where": {"mature":true, "categoryId": 1004}, "offset": 0, "limit": 5, "order": "gameName ASC" }
这将返回一个新的(第 1 页,因为我将偏移量设置回 0)游戏数组,按字母顺序按 gameName 排序:
[
{
"mature": true,
"gameName": "Battlefield4",
"categoryId": 1004,
"gameId": 1000055,
"description": "Published by EA Digital Illusions",
"publishedDate": "2013-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "CODBlacOps3",
"categoryId": 1004,
"gameId": 1000053,
"description": "Published by Activision",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Destiny",
"categoryId": 1004,
"gameId": 1000057,
"description": "Published by Bungie",
"publishedDate": "2014-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Evolve",
"categoryId": 1004,
"gameId": 1000054,
"description": "Published by Turtle Rock Studios",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Rainbow6",
"categoryId": 1004,
"gameId": 1000056,
"description": "Published by EUbisoft",
"publishedDate": "2015-01-01T00:00:00.000Z"
}
]
我之所以能够如此迅速地得到这个结果,是因为使用了 API Explorer。我分叉了您的项目,更改了数据库连接值以匹配我的本地服务器,启动它,然后转到 http://0.0.0.0/explorer/#!/Games/Games_find,然后在 Games 模型上使用不同的 filter 值: