Pretty HABTM List Entry

2011-08-28 Thread Mondo
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


One-way relationship

2011-05-30 Thread Mondo
I have Items and Units, and want each Item to have one Unit, but that
unit can be used by many.

Items Table
id - int
name - varchar
description varchar
unit_id - int

Units Table
id - int
name - varchar
description - varchar


Items Model
var $hasOne = array( 'Units' = array( 'className' = 'Unit' ) );

Units Model
Nothing, but previously I tried:  var $belongsTo = array( 'Items' =
array( 'className' = 'Item' ) );


When I'm in the scaffolding (List Items) I get:
Warning (512): SQL Error: 1054: Unknown column 'Units.item_id' in 'on
clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 684]

And in the Edit Item screen, the Units drop-down is empty. Any idea
what I'm doing wrong? Essentially the Units table are just to populate
a drop-down in the Item entry form, but I'd like it to auto-populate
in scaffold.

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php