我有一個食譜,項目和單位表/模型。我與配方和項目有一個HABTM關係,當添加/編輯配方時,我得到默認的多選框。 (大部分情況下,我都在使用Bake)。問題是我需要將數量和單位與每個項目相關聯。漂亮的HABTM列表條目
樣品UI的我希望的:
它的一個重要組成部分是添加/刪除/編輯單個項目的能力。我想象看看提交的表單數據,並使用一些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'
)
);
}
據我所知,CakePHP的不允許你對HABTM關係的性質,所以你應該從HABTM改變你的第一個2個模型的hasMany關係(它們鏈接到RecipesItem )。稍後,您可以烘烤RecipesItem的MVC,您將完成所有工作。要完成你提出的問題,你可以將烘焙添加視圖「移動」到食譜添加方法,從而對JS進行必要的更改。 – tvdias