[英]Grails with custom many to many relationship, relational table with parameters
我有以下域:評論,Secuser和評級。
我希望每個評論可由SecUser +1或-1評分一次。 在相關評論(針對領域討論)的視圖中,我希望有一個按鈕可以向上或向下投票並刷新視圖。 我到目前為止的代碼:注釋視圖:
<tbody>
<g:each in="${comments}" var="comm">
<tr>
<td>${comm.comment}</td>
<td>${comm.commentBy}</td>
<td><g:formatDate format="dd.MM.yyyy HH:mm" date="${comm.createDate}"/> </td>
<td><button type="button" class="btn btn-default btn-lg" onclick="${remoteFunction(action: 'ratePositiveComment', update: 'content', params:[commentID:"${comm.id}"])}"><span class="glyphicon glyphicon-thumbs-up" aria-hidden="false" style="float:right">${comm.numberPositiveRatings}</span></button>
<button type="button" class="btn btn-default btn-lg" onclick="${remoteFunction(action: 'rateNegativeComment', update: 'content', params:[commentID:"${comm.id}"])}"><span class="glyphicon glyphicon-thumbs-down" aria-hidden="false" onclick="addRating(-1, ${comm.id})" style="float:right">${comm.numberNegativeRatings}</span></button>
</td>
</tr>
</g:each>
</tbody>
評論控制器:
class Comments {
static belongsTo = Discussion
Discussion discussion
SecUser commentBy
String comment
Date createDate = new Date()
static hasMany = [commRatings : Rating]
public long getNumberPositiveRatings() {
return Rating.countByCommentRatedAndRate(this, 1);
}
public long getNumberNegativeRatings() {
return Rating.countByCommentRatedAndRate(this, -1);
}
List raters() {
return commRatings.collect{it.ratingUser}
}
List addToPosRatingUser(SecUser user) {
Rating.positiveRating(user, this)
//return raters()
}
List addToNegRatingUser(SecUser user) {
Rating.negativeRating(user, this)
// return raters()
}
評級域:
class Rating {
static belongsTo = Comments
int rate
SecUser ratingUser
Comments commentRated
static Rating positiveRating(ratingUser, commentRated) {
def m = Rating.findByRatingUserAndCommentRated(ratingUser, commentRated)
if (!m) {
m = new Rating()
ratingUser?.addToRating(m)
commentRated?.addToRating(m)
m.rate = 1;
m.save()
}
return m
}
static Rating negativeRating(ratingUser, commentRated) {
def m = Rating.findByRatingUserAndCommentRated(ratingUser, commentRated)
if (!m) {
m = new Rating()
ratingUser?.addToRating(m)
commentRated?.addToRating(m)
m.rate = -1;
m.save()
}
return m
}
}
在域SecUser(Spring安全插件)中,我添加了:
class SecUser {
transient springSecurityService
String username
String password
String userEmail
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static hasMany = [ratings:Rating]
List ratedComments() {
return ratings.collect{it.commentRated}
}
List addPosCommentRating(Comments comm) {
Rating.positiveRating(this, comm)
return ratedComments()
}
List addNegCommentRating(Comments comm) {
Rating.negativeRating(this, comm)
return ratedComments()
}
CommentsController是腳手架,唯一添加的功能是:
def ratePositiveComment() {
def rater = SecUser.findById(springSecurityService.currentUser.id);
if(rater!=null) {
Comments comm = Comments.get(params.commentID);
comm.addToPosRatingUser(rater);
comm.save();
}
}
def rateNegativeComment() {
def rater = SecUser.findById(springSecurityService.currentUser.id);
if(rater!=null) {
Comments comm = Comments.get(params.commentID);
comm.addToNegRatingUser(rater);
comm.save();
}
}
我得到的錯誤代碼是:
No signature of method: ForumProject.SecUser.addToRating() is applicable for argument types: (ForumProject.Rating) values: [ForumProject.Rating : (unsaved)]
Possible solutions: addToRatings(java.lang.Object). Stacktrace follows:
Message: No signature of method: ForumProject.SecUser.addToRating() is applicable for argument types: (ForumProject.Rating) values: [ForumProject.Rating : (unsaved)]
Possible solutions: addToRatings(java.lang.Object)
addTo
(和removeFrom
)方法是從hasMany
映射中的名稱派生的。 如果您已經聲明
static hasMany = [rating:Rating]
那么addTo
方法將是addToRating
並且您的代碼將是正確的。 現在,您只需要更改對addToRatings
的調用即可。
ps這很不好:
def rater = SecUser.findById(springSecurityService.currentUser.id);
springSecurityService.currentUser
已經是您想要的,用於登錄時用於創建Authentication
的SecUser
,它是通過查詢而不是從某些緩存從數據庫中檢索的。 但是您在獲得id
后將其丟棄,因此可以使用效率低下的動態查找器再次加載它。 始終使用get
而不是findById
。 所以你的代碼應該只是
def rater = springSecurityService.currentUser
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.