Saturday, December 27, 2008

I am missing something obvious

I dont know why I cant wrap my head around this. I have been doing
LAMP development for years yet CakePHP basics seem to be escaping me.
So here it is. I am working on a recipe management application. A
recipe's ingredient list is stored in the ingredients_list table. I am
having an issue figuring out how to add another ingredient in the add
recipe view and how to add or delete another ingredient in the edit
recipe view.

To add a new ingredient to the recipe I should just hit a button that
adds a new line to the form with all the fields needed for the
ingredient. I cant seem to figure out the right strategy using Cake. I
can do it with Javascript but thats not really the point.

To delete an ingredient there should be a button next to the line item
in the recipe view that calls a delete method on the ingredient_list
controller and then returns you to the recipe view that you were in.
When I call the delete method on the ingredient I get the following
from CakePHP:

Missing Method in RecipesController

Error: The action ingredient_list is not defined in controller
RecipesController

Error: Create RecipesController::ingredient_list() in file: app
\controllers\recipes_controller.php.

But I dont think that the delete method belongs there.

Help is much appreciated thanks.

Code.


RECIPES CONTROLLER


<?php
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', 'IngredientList');

function index()
{
$this->set('recipes', $this->Recipe->findAll());
$this->pageTitle = 'Recipes';
$this->layout = 'default';
}

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->saveAll($this->data))
{
$this->flash('Your recipe has been saved.', '/recipes');
}
}
}

function edit($id = null)
{
$this->Recipe->id = $id;
$this->set_recipe_types();
$this->set_ingredients();
$this->set_measurement_types();


$list = $this->IngredientList->find('all', array ('recipe_id' =>
$id));

//debug($list, true);

foreach($list as $item)
{
//debug($item['IngredientList']['recipe_id'], true);
if($item['IngredientList']['recipe_id'] == $id)
{
$ing_list[] = $item;
}
}

$this->set('ingredient_list', $ing_list);

if (empty($this->data))
{
$this->data = $this->Recipe->read();
}
else
{
if ($this->Recipe->saveAll($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')));
}
}

?>

INGREDIENT ELEMENT


<tr id="ingredient_<?php echo $index; ?>">
<td>
<?php
echo $form->input('IngredientList.'. $index .'.amount');
echo $form->input('IngredientList.'.$index.'.id', array
('type'=>'hidden'));
?>
</td>
<td>
<?php
echo $form->label('IngredientList.'.
$index .'.measurement_type_id', 'Measurement Type');
echo $form->select('IngredientList.'.
$index .'.measurement_type_id', $measurement_types);
?>
</td>
<td>
<?php echo $form->input('IngredientList.'.
$index .'.description'); ?>
</td>
<td>
<?php
echo $form->label('IngredientList.'. $index .'.ingredient_id',
'Ingredient');
echo $form->select('IngredientList.'. $index .'.ingredient_id',
$ingredients);
?>
</td>
<td>
<?php echo $html->link('delete', 'ingredient_list/delete'); ?>
</td>

</tr>


EDIT RECIPE VIEW


<h1>Edit Recipe</h1>
<?php
echo $form->create('Recipe', array('action' => 'edit'));
echo $form->input('Recipe.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>
<table id="ingredient_list">
<?php
$x = 0;
foreach($ingredient_list as $ingredient)
{
echo $this->element('add_ingredient', array('index' => $x));
$x++;
}
?>
</table>
<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');
?>


ADD RECIPE VIEW


<h1>Add Recipe</h1>
<?php
echo $form->create('Recipe');
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>
<table id="ingredient_list">
<?php
for($index = 0; $index < 10; $index ++)
{
echo $this->element('add_ingredient', array('index' => $x));
}
?>
</table>
<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 cake-php@googlegroups.com
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?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: