简体   繁体   中英

Play2 Map Objects in template

I have a play2 Project and I want to create an new Product. A Product includes a KeyInfo which is stored in a several Table.

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. The Problem is now, that the id for the KeyInformation is not stored in the 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并保存产品。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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