0

I'm new to CakePHP and I want to save additional data to my HABTM join table. I've read, that one should switch to hasMany through for this, but there is also the following note in the cake cookbook:

Changed in version 2.1.

You can set unique setting to keepExisting circumvent losing extra data during the save operation. See unique key in HABTM association arrays.

This sounds as if it is possible since 2.1. (I use the newest 2.4.2) Every question concerning this ('How to save additional data in HABTM relationships') are at least two years old. So is it possible nowadays to save additional data in HABTM join tables, e.g. ´ingredients_recipes.amount´ ("20ml of milk for whatEverRecipe")?

If so, how? As by now, the closest I got was NULL inserted for amount, or the amount was inserted instead of the recipes_id.

If not, how to convert a HABTM setup to hasMany through? I'd also be thankfull for a good and uptodate tutorial, that explains how to set it all up, as I don't really get what the cookbook writes.

Thanks

Nareille
  • 811
  • 2
  • 11
  • 30

1 Answers1

0

my data

Array ( [Recipe] => Array ( [id] => 58 ) [Ingredient] => Array ( [amount] => 50 cl [name]=>milk ) )

my view

<?php echo $this->Form->create('Ingredient');?>
    <?php echo $this->Form->input(
        'Recipe.id',
        array('type' => 'hidden', 'value' => $recipe_id)); ?>
    <?php echo $this->Form->input('Ingredient.name'); ?>
Form->input('Ingredient.amount'); ?> Form->end('Add Ingredient'); ?>

My function Add

public function add() {
    //save habtm
    if ($this->Ingredient->save($this->request->data)) {       
    }
}

With this code, you are associeted Amount with Recipe

Find this code on the cake book : And ctrl+f habtm

ColoO
  • 813
  • 5
  • 14
  • This association doesn't really make much sense, right? Why would you want to associate amounts, where the ingredient is hard-coded via the column name? o_O Also have you actually tested it? – ndm Oct 28 '13 at 17:01
  • Edited. It is just an example and i understand a little the english... Maybe i don t understand completely his problem :s. – ColoO Oct 28 '13 at 17:15
  • So it is possible to store additional data? as for the cookbook, I knew that section, but as it doesn't deal additional data, just the IDs. I'll try this later today (gotta go to work now), but doesn't `input('Ingredient.amount')` mean, `amount`is a column of my table `ingredients`? – Nareille Oct 29 '13 at 07:52