I have a site in cakephp 2.2, and I have a form with many input type text with a value taken from a query.
I want when I save with a submit button to create for every input text a new record into my table in my database.
First of all I have make a query a simple find and put it into a variable(prop). I make a foreach
and for every record that I have found I create an input text. But when I save I don't want to update but to create a new record into my table the value can be changed or not but I want to save each input type. I have try something but create only one record with value of the last input text and not for all.
My table have some field
- table name:
ingredient_properties
- model name:
IngredientProperty
- field:
id
,ingredient_id
,property_id
,user_id
,created
,modified
.
This is my view *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();
And this is my controller Ingredient.php (Ingredient
has many IngredientProperty
and Property
has many 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'));
}
}