2011-08-28 42 views
0

我有一個食譜,項目和單位表/模型。我與配方和項目有一個HABTM關係,當添加/編輯配方時,我得到默認的多選框。 (大部分情況下,我都在使用Bake)。問題是我需要將數量和單位與每個項目相關聯。漂亮的HABTM列表條目

樣品UI的我希望的:

sample

它的一個重要組成部分是添加/刪除/編輯單個項目的能力。我想象看看提交的表單數據,並使用一些jQuery和克隆將工作。但我想知道是否有人已經爲此創建了行爲?

現有型號(縮短到相關的東西,即刪除用戶/筆記/等):

class Item extends AppModel { 
    var $name = 'Item'; 

// id : int 
// name : varchar 
// unit_id : int 

    var $belongsTo = array(
     'Unit' => array(
      'className' => 'Unit', 
      'foreignKey' => 'unit_id' 
     ), 
    ); 

    var $hasAndBelongsToMany = array(
     'Recipe' => array(
      'className' => 'Recipe', 
      'joinTable' => 'recipes_items', 
      'foreignKey' => 'item_id', 
      'associationForeignKey' => 'recipe_id', 
     ) 
    ); 
} 

class Recipe extends AppModel { 
     var $name = 'recipe'; 
     var $displayField = "name"; 

// id : int 
// name : varchar 


     var $hasAndBelongsToMany = array(
      'Item' => array(
       'className' => 'Item', 
       'joinTable' => 'recipes_items', 
       'foreignKey' => 'recipe_id', 
       'associationForeignKey' => 'item_id', 
      ) 
     ); 
    } 

class RecipesItem extends AppModel { 
    var $name = 'RecipesItem'; 

// id : int 
// quantity : int 
// unit_id : int 
// recipe_id : int 
// item_id : int 



    var $belongsTo = array(
     'Unit' => array(
      'className' => 'Unit', 
      'foreignKey' => 'unit_id' 
     ), 
     'Recipe' => array(
      'className' => 'Recipe', 
      'foreignKey' => 'recipe_id' 
     ), 
     'Item' => array(
      'className' => 'Item', 
      'foreignKey' => 'item_id' 
     ) 
    ); 
} 
+0

據我所知,CakePHP的不允許你對HABTM關係的性質,所以你應該從HABTM改變你的第一個2個模型的hasMany關係(它們鏈接到RecipesItem )。稍後,您可以烘烤RecipesItem的MVC,您將完成所有工作。要完成你提出的問題,你可以將烘焙添加視圖「移動」到食譜添加方法,從而對JS進行必要的更改。 – tvdias

回答

0

不太清楚你在問什麼。要添加,編輯和刪除項目,您需要在項目控制器中創建操作。假設您的表單設置正確,則應通過控制器操作中的save()方法自動處理保存關聯數據(即配方有哪些項目)。

出於好奇,RecipesItem模型從哪裏來?這代表什麼?如果我正確理解你,你有一個食譜模型,和一個項目模型,與HABTM的關係。你不應該爲它們的連接表需要一個模型,recipes_items表只是關聯兩個模型中的項目。

+0

基本上是一個auto-'magic'(即烘焙或行爲),它將在模型中創建這種形式元素......如果它存在(或者很容易創建)。我做了第三個模型,希望我能夠定義一下這個field元素應該有更多的相關領域(仍然感受我通過Cake的方式),並且烘烤創建適當的輸入, – mondo

0

這不是蛋糕可以爲你做的事情。也許有些js可以幫助你,但你幾乎必須爲此編寫自己的javascript。