简体   繁体   English

模板中的Play2地图对象

[英]Play2 Map Objects in template

I have a play2 Project and I want to create an new Product. 我有一个play2项目,我想创建一个新产品。 A Product includes a KeyInfo which is stored in a several Table. 一个产品包括一个KeyInfo,它存储在多个表中。

This is my tempalte for an new Product 这是我对新产品的追求

@(newProductForm:Form[models.Product], keyInfoList: List[Keyinfo] )
@adminMain(""){
@helper.form(action = routes.Admin.insertNewProduct()) {

    @helper.inputText(
        newProductForm("name"),
        'label -> "name",
        'type -> "name"
    )

    @helper.inputText(
        newProductForm("price"),
        'label -> "price",
        'type -> "price"
    )

    @helper.inputText(
        newProductForm("shortDescription"),
        'label -> "shortDescription",
        'type -> "shortDescription"
    )

    @helper.inputText(
        newProductForm("description"),
        'label -> "description",
        'type -> "description"
    )


    @helper.select(
    newProductForm("keyinfo"),
    helper.options(
        for(info <- keyInfoList) yield info.keyinformation

        )
    )

    <button type="submit">Add</button>
 }
}

The select helper for the keyinfo is getting all Fieldnames correctly from the Table. keyinfo的选择助手正在从表中正确获取所有字段名称。 The Problem is now, that the id for the KeyInformation is not stored in the ProductTable. 现在的问题是,KeyInformation的ID未存储在ProductTable中。

Here is the Controller function to save a Product 这是保存产品的控制器功能

    public static Result insertNewProduct() {
    Form<Product> productForm = form(Product.class).bindFromRequest();
    return ok(showNewProduct.render(Product.create(productForm.get())));

    }

And the Product model with the create function 以及具有创建功能的产品模型

@Entity
public class Product extends Model {

@Id
//@Constraints.Required
//@Formats.NonEmpty
@Column(name="id")
public Integer id;

@Constraints.Required
public String name;

@Constraints.Required
public Float price;

@Constraints.Required
@Column(name="short_Description")
public String shortDescription;

@Constraints.Required
public String description;

@ManyToOne
@Constraints.Required
public Keyinfo keyinfo;



public static Product create(Product product){
    product.save();
    return product;
}

I hope that somebody can help me 我希望有人可以帮助我

Not sure of the root cause without seeing the whole project, but to help you find the issue, you can dump debug messages to check values while you attempt the save: 在没有看到整个项目的情况下不确定根本原因,但是为了帮助您找到问题,可以在尝试保存时转储调试消息以检查值:

public static Product create(Product product){
    play.Logger.info("Saving ... product name: " + product.name);
    play.Logger.info("Saving ... product key info: " + product.keyinfo.toString());
    ... 
    product.save();
    return product;
}

如果使用的是ebean,则可能需要先显式保存keyInfo,然后设置product.keyInfo并保存产品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM