由于我对Grails有点新手,我想知道如何迭代我保存在数据库中的当前数据以检查信息是否已经存在。
例如,假设我有一个Books类的域类,我创建了一个自动添加更多书籍的动作,但我想检查book.title是否已经存在,所以我不再添加它。
旁注 我只是使用默认数据库(项目设置为生产模式时使用的任何数据库)
修改
我会发布我的域名,因此更容易理解。而不是book.title,我把它改成了书属于作者的地方。所以我只希望作者添加一次,但能够为它添加许多书籍。问题发生在我在控制器中创建的操作中。
作者域名:
class Author {
static hasMany = [books:Book]
String authorName
String notes
String age
String toString() { authorName }
static constraints = {
authorName()
notes(maxSize:500)
age()
}
}
图书域名:
class Book {
static belongsTo = Author
String toString() { bookNumber }
Author bookAuthor
String title
String numberOfPages
static constraints = {
bookAuthor()
title()
numberOfPages()
}
}
Book Controller(这是我遇到问题的地方):
class BookController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
//took out index, create, list, etc. to focus on the once that I'm concerned about
//this action will simple read a text file and add books and authors
def gather = {
def parseData = new parseClient() //parses text file line by line and puts it into a list
def dataHolder = parseData.information //dataHolder will hold data from text file
int linesOfData = dataHolder.size() //get size to iterate and add authors & books
linesOfData.times {
def _author = dataHolder.author[it] //get author - Author
def _age = dataHolder.age[it] //get age - Author
def _title = dataHolder.title[it] //get title - Book
def _pages = dataHolder.pages[it] //get pages - Book
def authorInstance //create new Author to add
authorInstance = new Author() //for some reason I have to create and save AuthorName (can't have other fields) before I can add my Book correctly
authorInstance.setAuthorName(_author)
authorInstance.save()
def bookInstance
bookInstance = new Book() //create a new Book to add
bookInstance.setBookAuthor(authorInstance)
bookInstance.setTitle(_title)
bookInstance.setNumberOfPages(_pages)
bookInstance.save() //has to have access to the authorInstance to add correctly which is why i was wondering how to access the database to grab it if it existed
authorInstance.setAge(_age) //add whatever data is left for Author
authorInstance.save() //save again because cant save it with this information before I add authorInstance to the Book
}
}
}
文本文件内容:
//You'll notice that author _Scott Davis_ is in here twice.
//I don't want to add two instances of Scott Davis but need to access it to add the book
//the unique constraint makes the value come up as not null but can't be added
Scott Davis : Groovy Recipes
Bashar Abdul Jawad : Groovy and Grails Recipes
Fergal Dearle : Groovy for Domain-Specific Languages
Scott Davis : GIS for Web Developers: Adding 'Where' to Your Web Applications
所以我基本上都在寻找一种方法来添加这些信息,而且还没有找到一种似乎无法解决随机问题的方法。
希望这会稍微澄清我的问题,因为最初的问题有点宽泛
答案 0 :(得分:3)
在这种情况下,您可以对标题添加唯一约束,Grails将为您执行此操作。
您可以通过数据进行迭代,但如果可以避免,则可能不想加载数据库。因此,您可以编写自定义查询以选择标题的书籍数量,例如。
修改:供您更新
您无需在控制器中使用setter。 Grails在运行时为您动态添加setter / getters。如果你想在你的setter中添加一些逻辑,那么你可以定义自己的逻辑并在那种情况下使用它们
你看过grails文档了吗? http://grails.org/doc/latest/
您有一个静态约束块,但尚未定义如何约束每个属性。对于唯一标题,它将是
title(unique:true)
如果您想获得作者姓名列表,可以
List names = Author.executeQuery('select authorName from Author')