我对 saveAssociated 和存在条目有一点问题:(
楷模:
成分.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' => ''
)
);
}
食谱.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' => ''
)
);
}
成分配方.php
<?php
class IngredientRecipe extends AppModel {
public $name = 'IngredientsRecipes';
public $belongsTo = array('Ingredient', 'Recipe');
}
意见:
查看/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(); ?>
控制器:
成分配方控制器.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 ;
我的问题:
我如何在食谱和成分字段中保留现有数据并继续在成分食谱字段中保存相关数据?