迭代时访问Grails中的另一个数据库表
我有这些模型 Game.groovy迭代时访问Grails中的另一个数据库表,grails,gorm,Grails,Gorm,我有这些模型 Game.groovy class Game { String gameTitle static hasMany = [screenshot: Screenshots list:GameList] } GameList.groovy class GameList { static belongsTo = [game : Game , category : GameCategory] static constraints = { } }
class Game {
String gameTitle
static hasMany = [screenshot: Screenshots list:GameList]
}
GameList.groovy
class GameList {
static belongsTo = [game : Game , category : GameCategory]
static constraints = {
}
}
GameCategory.groovy
class GameCategory {
String categoryName
String icon
String toString(){
"${categoryName}"
}
static hasMany = [ list:GameList]
static constraints = {
}
}
在我的控制器中
GameController.groovy
def listGame(){
def category = GameCategory.list()
def currentCategory = params.categoryName
def myCategory=GameCategory.findByCategoryName(currentCategory)
def game = GameList.findAllByCategory(myCategory).collect{it.game}
[currentCategory:currentCategory, category:category, game:game]
}
在我看来,我迭代了所有游戏,以显示特定类别中的每个游戏。
此外,我想显示每个游戏的所有类别。如何在每次迭代中访问每个游戏的类别?如果您需要每个游戏的列表以及与该游戏相关的所有
游戏类别的列表:
def gamesAndCategories = Game.list().collect{ Game g ->
def matchingCategories = GameList.findAllByGame(g)*.category
return [game: g, categories: matchingCategories]
}
如果您想为游戏
的特定子集关联游戏类别
,可以将游戏.list()
替换为游戏
的集合
然后,您可以将games和categories
添加到现有的返回地图中,并在视图中使用它。使用域模型,您无法从game
实例访问游戏类别,因为无法将关联从game
导航到gamescategory
。根据相同的域模型,这里有一个漏洞:
def listGame(){
def category = GameCategory.list()
def currentCategory = params.categoryName
def myCategory=GameCategory.findByCategoryName(currentCategory)
def game = GameList.findAllByCategory(myCategory).collect{it.game}
def gameCategories = GameList.withCriteria {
inList 'game', game
projections {
property 'game'
property 'category'
}
}.groupBy { it[0] }
.collectEntries { game, record -> [(game): record*.getAt(1)] }
[currentCategory:currentCategory, category:category, game:game, gameCategories: gameCategories]
}
gameCategories
是一个Map
,粗略地说,它包含Game
变量中每个Game
的所有gameCategories
s。您可以将其传递到视图,并使用它查找游戏的GameCategory
s:def categories=gameCategories[gameInstance]
但是,您可以通过使用不同的域模型简单地实现所有这些,如下所示:
另一种域模型
现在,一个Game
有许多GameCategory
s,而GameCategory
有许多Game
s。有了这样的模型,listGame()
可以简化:
def listGame(){
def currentCategory = params.categoryName
def myCategory=GameCategory.findByCategoryName(currentCategory)
def games = myCategory.games
[currentCategory:currentCategory, games: games]
}
然后,在您的视图中,您可以如下方式渲染游戏和类别:
<g:each var="game" in="${games}">
<!-- Render game -->
<g:each var="category" in="${game.categories}">
<!-- Render the game's category -->
</g:each>
</g:each>
修改后的域模型才是真正的解决方案。很抱歉,游戏后的模型应该是静态hasMany=[screenshot:Screenshots list:GameList],当关系是多对多时,它不需要有关联实体吗?哪一个是GameList?不,gorm为您创建映射/连接表。GameList只是在碍事。没有它你会过得更好。Gorm可以直接处理类别和游戏之间的多对多。我在运行它时出错“加载插件管理器时出错:在多对多关系中的域类[class gameplify.game]和[class gameplify.GameCategory]之间未定义所有者。示例:static belongsTo=gameplify.GameCogory(使用--stacktrace查看完整跟踪)
<g:each var="game" in="${games}">
<!-- Render game -->
<g:each var="category" in="${game.categories}">
<!-- Render the game's category -->
</g:each>
</g:each>