0

我在 cakephp 2.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很多很多)IngredientPropertyPropertyingredientProperty

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'));

        }
    }
4

1 回答 1

1

尝试使用以下代码并询问它是否不适合您。

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;
于 2012-09-11T05:38:20.710 回答