for 循环中的 Grails AddTo

发布于 2024-09-14 12:07:37 字数 1627 浏览 8 评论 0原文

由于我是 grails 新手,我面临一个问题 我正在做一个用于阅读故事的网站,我现在的目标是将故事的内容保存到几个页面中以获取列表,然后轻松对其进行分页..所以我做了以下操作。

在域中,我创建了两个域,一个称为story,有这个:

class Story {

String title

List pages

static hasMany=[users:User,pages:Page]
static belongsTo = [User]

static mapping={
    users lazy:false
    pages lazy:false

}

}

,当然还有一个称为page的域,有这个:

class Page {
String Content
Story story
static belongsTo = Story
static constraints = {
    content(blank:false,size:3..300000)
}

}

,控制器保存方法如下:

 def save = {
    def storyInstance = new Story(params)
    def pages = new Page(params)
    String content = pages.content
    String[] contentArr = content.split("\r\n")

    int i=0

    StringBuilder page = new StringBuilder()
    for(StringBuilder line:contentArr){
        i++
        page.append(line+"\r\n")

        if(i%10==0){

               pages.content = page
               storyInstance.addToPages(pages)
               page =new StringBuilder()
        }
    }



    if (storyInstance.save(flush:true)) {
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'story.label', default: 'Story'), storyInstance.id])}"
        redirect(action: "viewstory", id: storyInstance.id)
    }else {
        render(view: "create", model: [storyInstance: storyInstance])
    }
}

我知道它看起来很乱,但它是一个原型..无论如何..问题是,每次条件为真时,我都在等待“storyInstance.addToPages(pages)”行将页面实例添加到页面集。但实际发生的情况是,它只给了我最后一个瞬间使用最后一个 page_idx,虽然我认为它应该逐个保存页面,这样我就可以获得每个故事的页面列表..

为什么会发生这种情况,是否有比我所做的更简单的方法..

我是在此等待任何值得赞赏的帮助

I am facing a problem due to that i'm newbie to grails
i'm doing a website for reading stories and my goal now is to do save the content of the story into several pages to get a list and then paginate it easily .. so i did the following.

in the domain i created two domains one called story and have this :

class Story {

String title

List pages

static hasMany=[users:User,pages:Page]
static belongsTo = [User]

static mapping={
    users lazy:false
    pages lazy:false

}

}

and have of course domain called page have this :

class Page {
String Content
Story story
static belongsTo = Story
static constraints = {
    content(blank:false,size:3..300000)
}

}

and the controller saving method gone like this:

 def save = {
    def storyInstance = new Story(params)
    def pages = new Page(params)
    String content = pages.content
    String[] contentArr = content.split("\r\n")

    int i=0

    StringBuilder page = new StringBuilder()
    for(StringBuilder line:contentArr){
        i++
        page.append(line+"\r\n")

        if(i%10==0){

               pages.content = page
               storyInstance.addToPages(pages)
               page =new StringBuilder()
        }
    }



    if (storyInstance.save(flush:true)) {
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'story.label', default: 'Story'), storyInstance.id])}"
        redirect(action: "viewstory", id: storyInstance.id)
    }else {
        render(view: "create", model: [storyInstance: storyInstance])
    }
}

i know it looks messy but it's a prototype..any way.. the problem is that i'm waiting from "storyInstance.addToPages(pages)" line to add to the set of pages an instance of the pages every time the condition is true..but what actually happen that it's give me the last instane only with the last page_idx while i thought it should save the pages one by one and so i can get a list of pages to every story..

why this happen and is there a simpler way to do it than what i did..

i'm waiting for any help here that is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文