具有自定义多对多关系的 Grails,具有参数的关系 table

Grails with custom many to many relationship, relational table with parameters

我有域:评论、安全用户和评级。

我希望每条评论都能被 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>
                &nbsp;
                <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.

p.s。这很糟糕:

def rater = SecUser.findById(springSecurityService.currentUser.id);

springSecurityService.currentUser 已经是你想要的,登录时用于创建 AuthenticationSecUser,它是通过查询从数据库中检索的,而不是从某些数据库中检索的缓存。但是你在得到它的 id 之后丢弃它,这样你就可以使用低效的动态查找器再次加载它。始终使用 get 而不是 findById。所以你的代码应该只是

def rater = springSecurityService.currentUser