私はこのGrailsから親を削除している間に子を削除しないようにする方法は?
Author.groovy
class Author {
String authorName;
String authorAddress;
String authorCNIC;
static hasMany = [books:Book]
static constraints = {
books(nullable: true)
}
}
ように私は、ドメインクラスを持っている
... 1人の著者が多くの本を持って、作者と本の間には多くの関係に1を持っているBook.groovy
class Book {
String bookName;
String Isbn;
static belongsTo = [author:Author]
static constraints = {
author(nullable: true)
}
}
この関数を呼び出すとき
def deleteauthor()
{
def id=params.id
Author author = Author.findById(id);
author.delete(flush: true)
render "Author Deleted"
}
それは著者とそのすべての子ブックを削除します。私はこの動作をしたくない、私はそれが著者と書籍を削除することはできませんし、メッセージを表示することはできませんこれを行う ?
あなたが行っている作者を見つけて、その本数を見ます(0より大きい場合)。削除を行わず、エラーメッセージを出します。 – railsdog
著者の書籍数を確認するには? – Ahmad
'著者作成者= .id(id)if(author.books?.size()> 0){レンダリング失敗(return)} author.delete(flush:true) レンダリング"著者削除 "はどうですか。しかし、たぶんあなたは 'カスケード'を見る必要がありますhttp://docs.grails.org/latest/ref/Database%20Mapping/cascade.html – Vahid