小编Yas*_*uri的帖子

Grails addTo*澄清

我正在关注Grails in Action中的示例.我有一个问题,了解该addTo*()功能的工作原理.

我有一个简单的域:User,Post,Tag具有以下关系:

  • 用户1对M发布
  • 用户1对M标签
  • 发布M-to-M标签

当我运行以下代码(第一种情况)时:

1.  def user = new User(userId: 'joe', password: 'secret').save()
2.  def tagGroovy = new Tag(name: 'groovy')
3.  def tagGrails = new Tag(name: 'grails')
4.  user.addToTags(tagGroovy)              
5.  user.addToTags(tagGrails)            
6.  
7.  def groovyPost = new Post(content: 'A groovy post')
8.  user.addToPosts(groovyPost)          
9.  groovyPost.addToTags(tagGroovy)
10.         
11. User.get(1).tags.each {println it.id + " " + it.name}
12. User.get(1).posts.each {println it.id + " " + it.content + " " + it.dateCreated}
Run Code Online (Sandbox Code Playgroud)

我明白了:

null grails
null groovy …
Run Code Online (Sandbox Code Playgroud)

grails flush grails-orm

7
推荐指数
1
解决办法
5720
查看次数

标签 统计

flush ×1

grails ×1

grails-orm ×1