I have 3 tables: recipes, ingredients and ingredients_recipes with
foreign keys set up. I was under the assumption that due to the
relationship if I saved my recipe the recipe ingredients would also be
saved but this is not the case. Would someone please straighten me
out? Thanks for any help.
class RecipesController extends AppController
{
/*
Make sure that if you add another controller that you put the
current controller
in the array as well or it wont be found.
*/
var $uses = array('Recipe', 'RecipeType', 'Ingredient',
'MeasurementType', 'IngredientsRecipes');
function index()
{
$this->set('recipes', $this->Recipe->findAll());
}
function view($id = null)
{
$this->Recipe->id = $id;
$this->set('recipe', $this->Recipe->read());
}
function add()
{
$this->set_recipe_types();
$this->set_ingredients();
$this->set_measurement_types();
if (!empty($this->data))
{
if ($this->Recipe->save($this->data))
{
$this->flash('Your recipe has been saved.',
'/recipes');
}
}
}
function edit($id = null)
{
$this->Recipe->id = $id;
$this->set_recipe_types();
if (empty($this->data))
{
$this->data = $this->Recipe->read();
}
else
{
if ($this->Recipe->save($this->data))
{
$this->flash('Your recipe has been
updated.','/recipes');
}
}
}
function delete($id)
{
$this->Recipe->del($id);
$this->flash('The recipe with id: '.$id.' has been deleted.', '/
recipes');
}
private function set_recipe_types()
{
$this->set('recipe_types', $this->RecipeType->find('list', array
('fields'=>'RecipeType.recipe_type',
'order'=>'RecipeType.recipe_type')));
}
private function set_ingredients()
{
$this->set('ingredients', $this->Ingredient->find('list', array
('fields'=>'ingredient', 'order'=>'ingredient')));
}
private function set_measurement_types()
{
$this->set('measurement_types',
$this->MeasurementType->find('list',
array('fields'=>'measurement_type', 'order'=>'measurement_type')));
}
}
CREATE TABLE `recipes` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`recipe` varchar(64) NOT NULL DEFAULT '',
`description` varchar(255) DEFAULT NULL,
`nutritional` text,
`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 `FK_recipe_type_id` FOREIGN KEY (`recipe_type_id`)
REFERENCES `recipe_types` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
CREATE TABLE `ingredients` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`ingredient` varchar(64) NOT NULL DEFAULT '',
`ingredient_type_id` int(10) unsigned zerofill NOT NULL,
`description` varchar(255) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_ingredient` (`ingredient`),
KEY `ingredient_type_id` (`ingredient_type_id`),
CONSTRAINT `ingredient_type_id` FOREIGN KEY (`ingredient_type_id`)
REFERENCES `ingredient_types` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
CREATE TABLE `ingredients_recipes` (
`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,
KEY `FK_ingredients_id` (`ingredient_id`),
KEY `FK_measurement_type_id` (`measurement_type_id`),
KEY `FK_recipe_id` (`recipe_id`),
CONSTRAINT `FK_ingredients_id` FOREIGN KEY (`ingredient_id`)
REFERENCES `ingredients` (`id`),
CONSTRAINT `FK_measurement_type_id` FOREIGN KEY
(`measurement_type_id`) REFERENCES `measurement_types` (`id`),
CONSTRAINT `FK_recipe_id` FOREIGN KEY (`recipe_id`) REFERENCES
`recipes` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---