在Grails中,如何使用视图和控制器中的中间联接类处理多对多?

在Grails中,如何使用视图和控制器中的中间联接类处理多对多?,grails,Grails,假设我有以下域: class Store { String name static hasMany = [ products: StoreProduct ] } class Product { String name static hasMany = [ stores: StoreProduct ] } class StoreProduct { BigDecimal discount static belongsTo = [ store:

假设我有以下域:

class Store {
    String name

    static hasMany = [ products: StoreProduct ]
}

class Product {
    String name

    static hasMany = [ stores: StoreProduct ]
}

class StoreProduct {
    BigDecimal discount

    static belongsTo = [ store: Store, product: Product ]
    static mapping = {
        id composite: ['store', 'product']
    }
换句话说,
Store
Product
之间存在多对多关系,中间的
StoreProduct
域类跟踪每家商店的单个折扣

Grails内置了对一对多关系的支持,因此您可以传入具有正确字段名的ID列表,控制器将自动将ID解析为实体列表。然而,在这种情况下,它是一个多对多的中间域类

我在
Store
edit视图中尝试了以下代码,以允许用户选择产品列表:

<g:each in="${products}" var="product" status="i">
    <label class="checkbox">
        <input type="checkbox" name="products" value="${product.id}"/>
         ${product.name}
    </label>
</g:each>
但它们都不能正常工作


我的问题是,Grails中是否有对这种关系的内置支持,特别是在视图方面?

来自上面的域类:-

  • 域(
    存储
    产品
    )中的任何一个都必须通过将
    归属于
    来承担关系的责任。有关详细信息,请参阅

  • 具有
    复合
    主键的域类(
    StoreProduct
    )应实现
    Serializable


如果可用,请提供stacktrace以进行调试。

按如下方式更改域结构:

class Store {
    String name

    Set<Product> getProducts() {
       StoreProduct.findAllByStore(this)*.product
    }
}

class Product {
    String name
    Set<Store> getStores() {
        StoreProduct.findAllByProduct(this)*.store
    }
}

import org.apache.commons.lang.builder.HashCodeBuilder
class StoreProduct implements Serializable {

    BigDecimal discount
    Store store
    Product product

    static mapping = {
        id composite: ['store', 'product']
        version false
    }

boolean equals(other) {
    if (!(other instanceof StoreProduct)) {
        return false
    }

    other.store?.id == store?.id &&
        other.product?.id == product?.id
}

int hashCode() {
    def builder = new HashCodeBuilder()
    if (store) builder.append(store.id)
    if (product) builder.append(product.id)
    builder.toHashCode()
}
}
类存储{
字符串名
Set getProducts(){
StoreProduct.findAllByStore(本)*.产品
}
}
类产品{
字符串名
设置getStores(){
StoreProduct.findAllByProduct(此)*.store
}
}
导入org.apache.commons.lang.builder.HashCodeBuilder
类StoreProduct实现可序列化{
大十进制折扣
商店
产品
静态映射={
id组合:[“商店”、“产品”]
版本错误
}
布尔等于(其他){
如果(!(StoreProduct的其他实例)){
返回错误
}
其他.store?.id==store?.id&&
其他.product?.id==产品?.id
}
int hashCode(){
def builder=新的HashCodeBuilder()
if(store)builder.append(store.id)
if(产品)builder.append(产品id)
builder.toHashCode()
}
}

当涉及到视图时,不是这样。您将哪些产品作为
产品
?显示控制器正在发送的内容。谢谢,这很有帮助,但我仍然不知道如何处理视图。
class Store {
    String name

    Set<Product> getProducts() {
       StoreProduct.findAllByStore(this)*.product
    }
}

class Product {
    String name
    Set<Store> getStores() {
        StoreProduct.findAllByProduct(this)*.store
    }
}

import org.apache.commons.lang.builder.HashCodeBuilder
class StoreProduct implements Serializable {

    BigDecimal discount
    Store store
    Product product

    static mapping = {
        id composite: ['store', 'product']
        version false
    }

boolean equals(other) {
    if (!(other instanceof StoreProduct)) {
        return false
    }

    other.store?.id == store?.id &&
        other.product?.id == product?.id
}

int hashCode() {
    def builder = new HashCodeBuilder()
    if (store) builder.append(store.id)
    if (product) builder.append(product.id)
    builder.toHashCode()
}
}