我在cakehp2.2中有一个站点,我有一个表单,其中有许多输入类型的文本,其中的值取自查询。
当我用提交按钮保存时,我希望为每个输入文本在数据库中的表中创建一个新记录。
首先,我将一个查询作为一个简单的查找,并将其放入一个变量(prop)中。我制作了一个foreach
,对于我找到的每一条记录,我都会创建一个输入文本。但当我保存时,我不想更新,而是想在表中创建一个新记录,值可以更改也可以不更改,但我想保存每个输入类型。我尝试了一些方法,但只创建了一个具有上次输入文本值的记录,而不是所有记录。
我的桌子上有一些字段
- 表名:
ingredient_properties
- 型号名称:
IngredientProperty
- 字段:
id
、ingredient_id
、property_id
、user_id
、created
、modified
这是我的观点*add_prop.ctp*
echo $this->Form->create('Ingredient', array ('class' => 'form'));
echo'<p>ELENCO PROPRIETA''</p>';
foreach($prop as $p){
echo('<p>'.$p['Property']['PropertyAlias'][0]['alias']);
echo $this->Form->input('IngredientProperty.ingredient_id', array ('type'=>'hidden', 'value'=> $p['IngredientProperty']['ingredient_id'],'label'=> false, 'id' => 'id'));
echo $this->Form->input('IngredientProperty.property_id', array ('type'=>'hidden', 'value'=> $p['IngredientProperty']['property_id'],'label'=> false, 'id' => 'id'));
echo $this->Form->input('IngredientProperty.value', array ('label'=> false, 'id' => 'value_'.$p['IngredientProperty']['id'], 'name' => 'value_'.$p['IngredientProperty']['id'],'value'=> $p['IngredientProperty']['value'], 'after' => '<div class="message">Inserisci un valore</div>'));
echo('</p>');
}
echo $this->Form->submit('Salva', array('id'=>'add_prop'));
echo $this->Form->end();
这是我的控制器Ingredient.php(Ingredient
有很多IngredientProperty
,Property
有很多ingredientProperty
)
public function add_prop($alias) {
if ($this->request->is('post'))
{
//SAVE
if ($this->Ingredient->IngredientProperty->saveAll($this->request->data)){
$this->set('flash_element','success');
$this->Session->setFlash ('Proprietà aggiornate con successo.');
//$this->redirect(array('action'=>'photo',$alias));
}
else{
$this->set('flash_element','error');
$this->Session->setFlash('Errore di salvataggio proprietà');
}
}
else{
//QUERY TO RETRIEVE DATA
$this->set('prop',$this->Ingredient->IngredientProperty->find('all', array('recursive' => 2,
'conditions' => array('IngredientProperty.ingredient_id' => $id))));
$this->loadModel('Property');
$this->set('prop_all',$this->Property->find('all'));
$this->loadModel('Unit');
$this->set('unit',$this->Unit->find('all'));
}
}
尝试使用以下代码,并询问它是否对您无效。
echo $this->Form->create('Ingredient', array ('class' => 'form'));
echo'<p>ELENCO PROPRIETA''</p>';
foreach($prop as $p){
echo('<p>'.$p['Property']['PropertyAlias'][0]['alias']);
echo $this->Form->input('IngredientProperty.ingredient_id][', array ('type'=>'hidden', 'value'=> $p['IngredientProperty']['ingredient_id'],'label'=> false, 'id' => 'id'));
echo $this->Form->input('IngredientProperty.property_id][', array ('type'=>'hidden', 'value'=> $p['IngredientProperty']['property_id'],'label'=> false, 'id' => 'id'));
echo $this->Form->input('IngredientProperty.value][', array ('label'=> false, 'id' => 'value_'.$p['IngredientProperty']['id'], 'name' => 'value_'.$p['IngredientProperty']['id'],'value'=> $p['IngredientProperty']['value'], 'after' => '<div class="message">Inserisci un valore</div>'));
echo('</p>');
}
echo $this->Form->submit('Salva', array('id'=>'add_prop'));
echo $this->Form->end();
并通过将表格请求的数据检查到您的控制器中
pr($this->request->data);
die;