Pretty HABTM List Entry

非 Y 不嫁゛ 提交于 2019-12-25 03:50:23

问题


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:

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',
                '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'
        )
    );
}

回答1:


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.




回答2:


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.




回答3:


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/



来源:https://stackoverflow.com/questions/7223072/pretty-habtm-list-entry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!