我正在关注Grails in Action中的示例.我有一个问题,了解该addTo*()功能的工作原理.
我有一个简单的域:User,Post,Tag具有以下关系:
当我运行以下代码(第一种情况)时:
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)