saveAssociatedに少し問題があり、エントリが存在します:(
モデル:
Ingredient.php
<?php
class Ingredient extends AppModel {
public $name = 'Ingredient';
public $hasMany = array('IngredientsRecipes');
public $hasAndBelongsToMany = array(
'Recipe' =>
array(
'className' => 'Recipe',
'joinTable' => 'ingredients_recipes',
'foreignKey' => 'ingredient_id',
'associationForeignKey' => 'recipe_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
Recipe.php
<?php
class Recipe extends AppModel {
public $name = 'Recipe';
public $hasMany = array('IngredientsRecipes');
public $hasAndBelongsToMany = array(
'Ingredient' =>
array(
'className' => 'Ingredient',
'joinTable' => 'ingredients_recipes',
'foreignKey' => 'recipe_id',
'associationForeignKey' => 'ingredient_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
IngredientRecipe.php
<?php
class IngredientRecipe extends AppModel {
public $name = 'IngredientsRecipes';
public $belongsTo = array('Ingredient', 'Recipe');
}
ビュー:
View / IngredientsRecipes / add.ctp
<?php echo $this->Form->create('IngredientRecipe'); ?>
<?php echo $this->Form->input('Ingredient.ingredient', array('type' => 'text', 'label' => 'Ingredient')); ?>
<?php echo $this->Form->input('Recipe.recipe', array('type' => 'text', 'label' => 'Recipe')); ?>
<button type="submit">Save</button>
<?php echo $this->Form->end(); ?>
コントローラー:
IngredientRecipeController.php
<?php
class IngredientRecipeController extends AppController {
public function add() {
if ($this->request->is('post')) {
if(!empty($this->request->data)) {
$ingredients_comma_separated = explode(',', $this->request->data['Ingredient']['ingredient']);
$recipes_comma_separated = explode(',', $this->request->data['Recipe']['recipe']);
$recipes = array();
foreach($ingredients_comma_separated as $ingredient){
$recipes['Ingredient']['ingredient'] = trim($ingredient);
foreach($recipes_comma_separated as $recipe){
$recipes['Recipe']['recipe'] = trim($recipe);
if ($this->IngredientRecipe->saveAssociated($recipes, array('deep' => true, 'validate' => 'first'))) {
$this->Session->setFlash('Saved.');
}
}
}
}
}
}
}
MySQLテーブル:
CREATE TABLE IF NOT EXISTS `ingredients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ingredient` varchar(250) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `ingredient` (`ingredient`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `ingredients_recipes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ingredient_id` int(11) NOT NULL,
`recipe_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `recipes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`recipe` varchar(250) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `recipe` (`recipe`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
私の質問:
レシピと材料のフィールドに既存のデータを保持し、関連するデータを材料のレシピのフィールドに保存し続けるにはどうすればよいですか?