php - cakePHP 3.0 HABTM relations save data -
i want make tutorial http://miftyisbored.com/complete-tutorial-habtm-relationships-cakephp/ in cakephp 3.0
i have 3 tables: recipes, ingredients , ingredients_recipes.
when making recipe, want select ingredients. want store recipe_id , ingredient_id in ingredients_recipes table, fail so. think there's wrong in recipescontroller. can me or point me in right direction?
problem:
$ingredients = $this->recipes->ingredients->find('list', ['limit' => 200]); // => gives me message "the recipe not saved. please, try again." $ingredients = $this->ingredients->find('list', ['limit' => 200]); // => gives me error "call member function find() on boolean"
when var dump (when using $this->recipes->ingredients->find) this:
array(3) { ["recipe_name"]=> string(4) "test" ["recipe_description"]=> string(4) "test" ["recipe"]=> array(1) { ["ingredient"]=> array(1) { [0]=> string(1) "1" } } }
tables:
create table `recipes` ( `recipe_id` int(11) not null auto_increment, `recipe_name` varchar(255) not null, `recipe_description` text not null, primary key (`recipe_id`) ); create table `ingredients` ( `ingredient_id` int(11) not null auto_increment, `ingredient_name` varchar(255) not null, `ingredient_description` text not null, primary key (`ingredient_id`) ); create table `ingredients_recipes` ( `ingredient_id` int(11) not null, `recipe_id` int(11) not null, primary key (`ingredient_id`,`recipe_id`) );
here's code below:
model > entity:
recipe
class recipe extends entity { protected $_accessible = [ 'recipe_id' => true, 'recipe_name' => true, 'recipe_description' => true, ]; }
ingredient
class ingredient extends entity { protected $_accessible = [ 'ingredient_id' => true, 'ingredient_name' => true, 'ingredient_description' => true, ]; }
ingredientsrecipe
class ingredientsrecipe extends entity { protected $_accessible = [ 'ingredient' => true, 'recipe' => true, ]; }
model > table :
recipestable
class recipestable extends table { public function initialize(array $config) { $this->table('recipes'); $this->displayfield('recipe_name'); $this->primarykey('recipe_id'); $this->belongsto('recipes', [ 'foreignkey' => 'recipe_id', 'jointype' => 'inner' ]); $this->belongstomany('ingredients', [ 'classname' => 'ingredients', 'jointable' => 'ingredients_recipes', 'foreignkey' => 'recipe_id', 'targetforeignkey' => 'ingredient_id' ]); } public function validationdefault(validator $validator) { $validator ->requirepresence('recipe_name', 'create') ->notempty('recipe_name') ->requirepresence('recipe_description', 'create') ->notempty('recipe_description') ->requirepresence('ingredients', 'create') ->notempty('ingredients'); return $validator; } }
ingredientstable
class ingredientstable extends table { public function initialize(array $config) { $this->table('ingredients'); $this->displayfield('ingredient_name'); $this->primarykey('ingredient_id'); $this->belongsto('ingredients', [ 'foreignkey' => 'ingredient_id', 'jointype' => 'inner' ]); $this->belongstomany('recipies', [ 'classname' => 'recipies', 'jointable' => 'ingredients_recipes', 'foreignkey' => 'ingredient_id', 'targetforeignkey' => 'recipe_id' ]); } }
ingredientsrecipestable
class ingredientsrecipestable extends table { public function initialize(array $config) { $this->table('ingredients_recipes'); $this->displayfield('recipe_id'); $this->primarykey(['recipe_id', 'ingredient_id']); $this->belongsto('recipies', [ 'foreignkey' => 'recipe_id', 'jointype' => 'inner' ]); $this->belongsto('ingredients', [ 'foreignkey' => 'ingredient_id', 'jointype' => 'inner' ]); }
controller:
recipescontroller
public function add() { $recipe = $this->recipes->newentity(); if ($this->request->is('post')) { $recipe = $this->recipes->patchentity($recipe, $this->request->data); // var_dump($this->request->data); if ($this->recipes->save($recipe)){ $this->flash->success('the recipe has been saved.'); return $this->redirect(['action' => 'index']); } else { $this->flash->error('the recipe not saved. please, try again.'); } } $recipes = $this->recipes->find('list', ['limit' => 200]); $ingredients = $this->recipes->ingredients->find('list', ['limit' => 200]); // => gives me message "the recipe not saved. please, try again." $ingredients = $this->ingredients->find('list', ['limit' => 200]); // => gives me error "call member function find() on boolean" $this->set(compact('recipe', 'recipes', 'ingredients')); $this->set('_serialize', ['recipe']); }
template > recipes
add.ctp
<?= $this->form->create($recipe); ?> <fieldset> <legend><?= __('add recipe') ?></legend> <?php echo $this->form->input('recipe_name', array( 'label' => 'name' ) ); echo $this->form->input('recipe_description', array( 'label' => 'description' ) ); echo $this->form->input('recipes.ingredients', ['multiple'=>true]); ?> </fieldset> <?= $this->form->button(__('submit')) ?> <?= $this->form->end() ?>
maybe tutorial help: http://book.cakephp.org/3.0/en/tutorials-and-examples/bookmarks/intro.html.
it explains how set habtm relations, adding tags bookmarks, , more.
good luck , happy coding :)
Comments
Post a Comment