1

我正在尝试使用 Fixture 插件在 Grails 应用程序中加载数据。

class Author {
  String name 
}

class Book {
  String title
  hasMany = [authors:Author]
}

我将作者加载到一个单独的文件中,并将其包含在第一本书中。

//author fixture
fixture {
  author1(Author) {
    name = 'Ken Follett'
  }
  author2(Author) {
    name = 'Martin Fowler'
  }
}

//book fixture
fixture {
  include 'author'
  "book"(Book) {
    title = 'Your favorite book'
    authors = [author1, author2]
  }
}

这一切都很好。我不能做的是替换 [author1, author2] 以便我可以动态(和随机)分配作者。就像是:

def dynamicallyBuiltAuthorList = "author1, author2, author100"
authors = ["${dynamicallyBuiltAuthorList}"]

到目前为止,我尝试的几乎所有东西都给了我一个没有匹配的编辑器或找到转换策略的错误。

在此先感谢 Grails 大师!

4

2 回答 2

1

根据您在下面的答案(以及对此答案的先前编辑),这可能是一种更好的方法:

def dynamicallyBuiltAuthorList = [ 'author1', 'author2', 'author100' ].collect {
  ref( "$it" )
}
authors = dynamicallyBuiltAuthorList
于 2012-09-12T10:14:17.757 回答
0

最后答案很简单:

def authorList = []
def dynamicallyBuiltAuthorList = [ 'author1', 'author2', 'author100' ].collect {
   authorList.add(ref("$it"))
}
authors = authorList

感谢蒂姆的帮助!

于 2012-09-12T22:09:51.707 回答