我有一个评级系统,用户应该在每年的输入表中输入他的数据(列出所有年份)然后他立即保存所有输入数据,如果数据先前存在则系统应该显示输入字段中的数据,以便用户可以更新记录,如果该年份的数据不存在则输入字段应为空,系统应在用户在字段中输入值时添加新记录。 我有一些问题。数据没有在输入字段中显示,这是第一个问题。第二个问题是当我尝试保存数据时,我没有在数据库中获得任何新内容。
查看:
<table width="100%" style="font-size:12px">
<tr>
<th>Year</th>
<th>Value</th>
<th>Data Quality </th>
<th>Reference</th>
<th>Comment</th>
</tr>
<? foreach ($years as $year) :?>
<tr>
<td><? echo $year['ReportYear']['year'];?></td>
<? echo $this->Form->create('IndicatorDatum');?>
<? echo $this->Form->hidden('IndicatorDatum.id');?>
<td > <? echo $this->Form->input('IndicatorDatum.value', array('label'=> false)); ?></td>
<td> <? echo $this->Form->input("IndicatorDatum.Assurance",array(
'style'=>'width:250px; display: -webkit-inline-box;',
'type'=>'select',
'multiple'=>'checkbox',
'options'=>$assurances,
'label' => false)
); ?>
</td>
<td><? echo $this->Form->input('IndicatorDatum.comment',array(
'type' => 'textarea',
'escape' => false,
'label' => false));?>
</td>
<td><? echo $this->Form->input('IndicatorDatum.reference',array(
'type' => 'textarea',
'escape' => false,
'label' => false)); ?>
</td>
</tr>
<? endforeach ; ?>
</table>
控制器:
if ($this->request->is('post')|| $this->request->is('put')) {
if ($this->IndicatorDatum->saveMany($this->request->data['IndicatorDatum'])) {
$this->Session->setFlash(
__('The indicator data has been saved'),
'flash/success'
);
$this->redirect(array(
'action'=>'edit_group',
$group_id
));
} else {
$this->Session->setFlash(
__('The indicator data could not be saved. Please verify the fields highlighted in red and try again.'),
'flash/error'
);
}
}
答案 0 :(得分:2)
我试着向你展示如何实现目标。只需阅读以下代码,并尝试按照您的意愿实施。
// view
echo $this->Form->create('ReportYear', array('action' => 'add'));
// Use loop for multiple rows
// Here I use 0 for only one row
echo $this->Form->input('IndicatorDatum.0.value');
echo $this->Form->input('IndicatorDatum.0.comment');
echo $this->Form->input('IndicatorDatum.0.reference');
echo $this->Form->end('Save');
// in controller
public function add() {
if (!empty($this->request->data)) {
// Use the following to avoid validation errors:
unset($this->ReportYear->IndicatorDatum->validate['id']);
$this->Company->saveAssociated($this->request->data);
}
}
为了最好的理解,请阅读Saving Related Model Data (hasOne, hasMany, belongsTo) in CakePHP
<强>更新强>
echo $this->Form->input('IndicatorDatum.0.value', array(
'value'=>$data['IndicatorDatum'[0]['value'
)
);
这里我在输入字段中添加了value
属性。我只是提出了价值,但你需要在这里设置适当的价值