I am still having a problem with related models and I cant seem to
find the example that will help me figure it out.
I have tables: recipes, ingredients and ingredient_lists (the SQL for
these is below)
The combination of ingredients and their amounts are stored in the
ingredient_lists table for a recipe.
A recipe and it's ingredients_list are added or edited in the same
view.
THE PROBLEM.
The issues comes when I try to edit a recipe's ingredients. Instead of
updating the ingredient_lists table a new record is inserted.
I think that Cake needs to know what the ID of the ingredient is just
as it would the recipe ID. However I cant put more hidden fields on
the page with the key of 'id' as that would conflict with the recipe
hidden id field.
Can someone please give me a clue as to how to approach this? It's got
to be the most common thing in the world to do in a relational
database. Once I sort this out with your help I intend to write a
tutorial on saving related model data as this is something that is
missing from the CakePHP site.
Thanks in advance.
SQL
CREATE TABLE `recipes` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`recipe` varchar(64) NOT NULL DEFAULT '',
`description` varchar(255) DEFAULT NULL,
`instructions` text,
`servings` int(2) DEFAULT NULL,
`recipe_type_id` int(10) unsigned zerofill NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_recipe_type_id` (`recipe_type_id`),
CONSTRAINT `recipe_type_id` FOREIGN KEY (`recipe_type_id`)
REFERENCES `recipe_types` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
CREATE TABLE `ingredient_lists` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`recipe_id` int(10) unsigned zerofill NOT NULL,
`ingredient_id` int(10) unsigned zerofill NOT NULL,
`amount` int(5) unsigned NOT NULL,
`measurement_type_id` int(10) unsigned zerofill NOT NULL,
`description` varchar(255) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_ingredients_id` (`ingredient_id`),
KEY `FK_measurement_type_id` (`measurement_type_id`),
KEY `FK_recipe_id` (`recipe_id`),
CONSTRAINT `ingredients_id` FOREIGN KEY (`ingredient_id`) REFERENCES
`ingredients` (`id`),
CONSTRAINT `measurement_type_id` FOREIGN KEY (`measurement_type_id`)
REFERENCES `measurement_types` (`id`),
CONSTRAINT `recipe_id` FOREIGN KEY (`recipe_id`) REFERENCES
`recipes` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
Temporary Edit View
<h1>Edit Recipe</h1>
<?php
echo $form->create('Recipe', array('action' => 'edit'));
echo $form->input('id', array('type'=>'hidden'));
echo $form->input('Recipe.recipe');
echo $form->input('Recipe.servings', array('maxlength' => 5, 'size' =>
5));
echo $form->label('Recipe.servings', 'Recipe Type');
echo $form->select('Recipe.recipe_type_id', $recipe_types);
?>
<div>
<h2>Ingredient List</h2>
<?php
$x = 0;
foreach($ingredient_list as $ingredient)
{
?>
<table>
<tr>
<td><?php echo $form->input('IngredientList.'.$x.'.amount');
?></td>
<td><?php
echo
$form->label('IngredientList.'.$x.'.measurement_type_id',
'Measurement Type');
echo
$form->select('IngredientList.'.$x.'.measurement_type_id',
$measurement_types);
?></td>
<td><?php echo
$form->input('IngredientList.'.$x.'.description'); ?
></td>
<td><?php
echo $form->label('IngredientList.'.$x.'.ingredient_id',
'Ingredient');
echo
$form->select('IngredientList.'.$x.'.ingredient_id',
$ingredients);
?></td>
</tr>
</table>
<?php
$x++;
}
?>
<h2>Instructions and Description</h2>
<?php
echo $form->input('Recipe.description', array('rows' => '3'));
echo $form->input('Recipe.instructions', array('rows' => '3'));
echo $form->end('Save Recipe');
?>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---