Grails 控制器动态查找器

发布于 2024-11-09 15:29:29 字数 1223 浏览 0 评论 0原文

我对 Grails 有点陌生,所以我需要有关此事的帮助。想象一下我有这些类:

Class User{
String name
String nr

}

class Computer{

String processor
String ram
User user
}

Class Room{
String roomNr
Computer computer
}

我想做一个这样的控制器:

def print= {

        def user = User.get(1)

        def computer = Computer.findAllByUser(user) // all computers filtered by User

        [computer: computer]       
    }

控制器运行良好,但我希望不仅能够传递计算机实例,而且还能够传递具有该计算机 ID 的房间。我不知道该怎么做,但会是这样的(相同的控制器,相同的操作):

def print= {

            def user = User.get(1)

            def computer = Computer.findAllByUser(user) // all computers filtered by User
            def room = Room.findAllByComputer(computer) ##

            [computer: computer]       
        }

嗯,这是错误的,因为 ### 所在的位置,“计算机”代表 id 列表,而不是单个 id。当我传递要在 ag 内的 gsp 中打印的计算机列表时:每个标签如下所示:(it.Processor、it.Ram)。我不知道如何获取每个标签中使用当前计算机的所有房间。这有点难以解释,但基本上我的输出是:

<g:each in="${computer}"
Computer1:
Processor: ${it.processor}
Ram: ${it.ram}
Room: ????? (How to get here??)
</g:each>

PS。我无法进行一对多双向操作,因为如果我在房间表中有一个指向计算机表的外键,则意味着每个房间线路必须有一个计算机引用。这可能会发生,也可能不会。所以我正在寻找解决我的问题的方法。

I'm kinda new to Grails so I need help with this matter. Imagine I have these classes:

Class User{
String name
String nr

}

class Computer{

String processor
String ram
User user
}

Class Room{
String roomNr
Computer computer
}

I want to do a controller like this:

def print= {

        def user = User.get(1)

        def computer = Computer.findAllByUser(user) // all computers filtered by User

        [computer: computer]       
    }

The controller works good, but I want to be able to pass not only the computer instances, but also the Room's with that computer id's. I dont know how to do it, but would be something like this (same controller, same action):

def print= {

            def user = User.get(1)

            def computer = Computer.findAllByUser(user) // all computers filtered by User
            def room = Room.findAllByComputer(computer) ##

            [computer: computer]       
        }

Well, this is wrong because, where the ### is, 'computer' represents a list of id's and not a single id. As I am passing a list of computers to be printed in my gsp inside a g: each tag like this: (it. Processor, it. Ram). I dont know how to get all the rooms which are using the current computer in the each tag. This is a bit hard to explain, but basically my output would be:

<g:each in="${computer}"
Computer1:
Processor: ${it.processor}
Ram: ${it.ram}
Room: ????? (How to get here??)
</g:each>

PS. i cannot do it one-to-many bidirectional because, if i have a foreign key in the room table poitinng to computer table, that means each room line must have one computer reference. And that can happen or not. So im looking for a solution for my issue.

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

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

发布评论

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