2012-11-25 67 views
3

我是新玩框架2.我有2個模型:書和作者,一本書可以有許多作者,所以我認爲它的許多。這裏是我的模型:玩框架2:節省很多很多

@Entity 
public class Book extends Model { 

    @Id 
    public Long id; 

    @Constraints.Required 
    public String title; 

    @Constraints.Required  
    @ManyToMany(cascade=CascadeType.ALL,mappedBy="books") 
    public Set<Author> authors = new HashSet<Author>(); 
    public static Model.Finder<Long,Book> find = new Model.Finder<Long,Book>(Long.class, Book.class); 

    public static Page<Book> page(int page, int pageSize, String sortBy, String order, String filter) { 
     return 
     find.where() 
      .ilike("title", "%" + filter + "%")     
      .orderBy(sortBy + " " + order)  
      .findPagingList(pageSize) 
      .getPage(page); 
    } 

} 

@Entity 
public class Author extends Model { 

    @Id 
    public Long id; 

    @Constraints.Required 
    public String name; 

    @ManyToMany(cascade=CascadeType.ALL) 
    public Set<Book> books = new HashSet<Book>(); 

    public static Model.Finder<Long,Author> find = new Model.Finder<Long,Author>(Long.class, Author.class); 

    public static Page<Author> page(int page, int pageSize, String sortBy, String order, String filter) { 
     return 
     find.where() 
      .ilike("name", "%" + filter + "%") 
      .orderBy(sortBy + " " + order)     
      .findPagingList(pageSize) 
      .getPage(page); 
    } 

} 

,我用這個選擇標籤的報名表,就像這樣:

<select name="authors.id" multiple="multiple"> 
    <option value="1">bejo</option> 
    <option value="2">joko</option> 
</select> 

這裏是我的控制器代碼:

Map<String, String> newData = new HashMap<String, String>(); 
Map<String, String[]> urlFormEncoded = play.mvc.Controller.request().body().asFormUrlEncoded(); 
if (urlFormEncoded != null) { 
    for (String key : urlFormEncoded.keySet()) { 
    String[] value = urlFormEncoded.get(key); 
    if (value.length == 1) {      
     newData.put(key, value[0]); 
    } else if (value.length > 1) { 
     for (int i = 0; i < value.length; i++) { 
     newData.put(key + "[" + i + "].id", value[i]);   
     } 
    } 
    } 
}   
Form<Book> bookForm = new Form<Book>(Book.class).bind(newData); 
if(bookForm.hasErrors()) { 
return badRequest(createForm.render(bookForm)); 
} 
bookForm.get().save(); 

但這些代碼不起作用。有些身體可以幫助我嗎? 感謝

回答

2

不要註釋你的關係作爲需要,而不是進行檢查,同時項目保存嘗試:也許現在

public static Result saveBook() { 
    Map<String, String[]> formUrlEncoded = request().body().asFormUrlEncoded(); 
    Form<Book> bookForm = form(Book.class).bindFromRequest(); 
    Set<Author> authors = new HashSet<Author>(); 

    // iterate through the keys to find values and pre-fill required Set(s) 
    for (String key : formUrlEncoded.keySet()) { 
     String[] values = formUrlEncoded.get(key); 
     for (String val : values) { 
      if ("authors.id".equals(key)) authors.add(Author.find.ref(Long.valueOf(val))); 
     } 
    } 

    // Check if form hasn't errors and if it contains authors 
    if (bookForm.hasErrors() || authors.size() < 1) { 
     return badRequest(createForm.render(bookForm)); 
    } 

    // Create a Book, fill with data from form, add relations, save 
    Book book = new Book(); 
    book = bookForm.get(); 
    book.authors = authors; 
    book.save(); 

    flash("generalInfo", "Book saved, thank you!"); 
    return redirect(routes.Application.index()); 
} 
+0

在2016年,我們有一個更好的方式來實現這一目標?找不到我 – cyril