Monday, August 29, 2011

Re: Pretty HABTM List Entry

As soon as you want extra fields in the join table you should be model-
ising that table.

- Recipe hasMany ItemRecipe
- Item hasMany ItemRecipe
- ItemRecipe belongsTo Recipe and Item

This way you can easily work with the extra fields in your join table
and run finds/paginates on it too.

On Aug 28, 7:51 pm, Mondo <mo...@nodegarden.net> wrote:
> 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:http://i.stack.imgur.com/Y4T6T.png
>
> 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'
>         )
>     );
>
>
>
>
>
>
>
> }

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: