在 Grails 中将 joinTables 呈现为列表

发布于 2024-11-11 15:25:31 字数 950 浏览 3 评论 0原文

我有两个域类:汽车和驾驶员。我想制作一个 gsp 视图,在列表中显示汽车和驾驶员信息,该列表看起来或多或少是默认的脚手架列表。例如:

Make  Model  Driver  Age
------------------------
Chevy Nova   Joe     20
Chevy Nova   Mike    30
Chevy Nova   Sally   40 

这是域类:

class Car {
  String make
  String model

  static constraints = { }
  static hasMany = [ drivers : Driver ]

  static mapping = {
    drivers joinTable: [name: 'Car_Driver', column: 'DRIVER_ID', key: 'CAR_ID']
  }
}

class Driver {
  String name
  int    age

  static constraints = { }  
}

制造一辆汽车并给它一些驱动程序:

def car = new Car(make: 'Chevy', model: 'Nova')
def driver1 = new Driver(name: 'Joe', age: 20)
def driver2 = new Driver(name: 'Mike', age: 30)
def driver3 = new Driver(name: 'Sally', age: 40)

car.addToDrivers(driver1)
car.addToDrivers(driver2)
car.addToDrivers(driver3)
car.save()

我需要在 CarController 和/或 gsp 视图中做什么才能发生连接列表(并且仍然可以使用分页)?

I've got two domain classes: Car and Driver. I'd like to make a gsp view that displays the Car and Driver info in a list that looks more or less a default scaffolded list. For example:

Make  Model  Driver  Age
------------------------
Chevy Nova   Joe     20
Chevy Nova   Mike    30
Chevy Nova   Sally   40 

Here's the domain classes:

class Car {
  String make
  String model

  static constraints = { }
  static hasMany = [ drivers : Driver ]

  static mapping = {
    drivers joinTable: [name: 'Car_Driver', column: 'DRIVER_ID', key: 'CAR_ID']
  }
}

class Driver {
  String name
  int    age

  static constraints = { }  
}

Make a car and give it a few drivers:

def car = new Car(make: 'Chevy', model: 'Nova')
def driver1 = new Driver(name: 'Joe', age: 20)
def driver2 = new Driver(name: 'Mike', age: 30)
def driver3 = new Driver(name: 'Sally', age: 40)

car.addToDrivers(driver1)
car.addToDrivers(driver2)
car.addToDrivers(driver3)
car.save()

What do I need to do in my CarController and/or gsp view to make a joined list happen (and still work with pagination)?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可遇━不可求 2024-11-18 15:25:32

如果 Driver 只能拥有一辆 Car,则您需要一个 Driver 来引用 Car 并仅渲染脚手架 < 驱动程序的code>list。

要调整列表列,您必须 grails生成视图

如果一个Driver可以有很多Cars,并且您不想将Car_Driver表拉入可见域模型(它没有自己的域含义) ),使用 SQL 查询结果作为 cardriverInstancesList 来创建类似脚手架的 list 操作。就像这里:Grails 中的 SQL/数据库视图

只需检查 result 是否为 PagedResultList。如果没有,您可以手动创建一个 PagedResultList,它可以轻松地由 ListtotalCount 构造而成,您可以使用 SQL 找到它们。

If a Driver can have only one Car, you need a Driver to reference Car and just to render scaffolding list for a Driver.

To tweak list columns, you'll have to grails generate-views.

If a Driver can have many Cars, and you don't want to pull Car_Driver table into visible domain model (it has no own domain meaning), make a scaffolding-like list action using SQL query result as a cardriverInstancesList. Like here: SQL/Database Views in Grails.

Just check that result is a PagedResultList. If not, you can create a PagedResultList by hand, it is easily constructed from a List and totalCount, which you can find with SQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文