开发者

Pretty HABTM List Entry

I have a Recipe, Item, and Units table/model. I have a HABTM relationship with Recipe and Item, and I get the default multiple-select box when adding/editing Recipe. (am using Bake for everything for the most part). The problem is I need to associate quantities and units with each Item.

Sample of UI I'm hoping for:

Pretty HABTM List Entry

A big component of it is the ability to add/delete/edit the individual items. I imagine looking at the submitted form data, and using some jquery and clone would work. But I was wondering if someone already created a Behavior perhaps for this already?

Current Models (shortened to the relevant stuff, ie removed users/notes/etc):

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',
   开发者_C百科             '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'
        )
    );
}


Not quite sure what you're asking. For adding, editing and deleting items you would need create actions in your items controller. Saving association data (ie which Items a Recipe has) should be handled more-or-less automatically by the save() method in your controller action, assuming you have your forms set up correctly.

Out of curiosity, where did the RecipesItem model come from? What does that represent? If I am understanding you correctly, you have a Recipe model, and an Item model, with HABTM relationship. You shouldn't need a model for their join table, the recipes_items table just relates items from the two models.


that's not something Cake can do for you. Maybe there's some js that can helps you a bit, but you'll pretty much have to write your own javascript for that.


You have to use javascript to "transform" the select tag into something "cooler".

Here is the jquery-multiselect plugin which I use quite a bit. You can easily set it up to replace all of your multi selects with 1 line of code.

More info here: http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜