cakephp - 在 Cakephp 中保存与同一模型相关的多个记录

标签 cakephp save cakephp-2.0

我有一个评级系统,用户应该在每年的输入表中输入他的数据(列出所有年份),然后他立即保存所有输入数据,如果数据之前存在,那么系统应该显示输入字段中的数据,以便用户可以更新记录,如果该年份的数据不存在,则输入字段应为空,并且当用户在字段中输入值时,系统应添加新记录。 我遇到了一些问题。数据没有显示在输入字段中,这是第一个问题。第二个问题是,当我尝试保存数据时,数据库中没有任何新内容。

查看:

<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>

Controller :

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

最佳答案

我尝试向您演示如何实现目标。只需阅读以下代码并尝试按照您的意愿实现即可。

// 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 属性。我只是输入值,但你需要在这里输入你想要看到的正确值

关于cakephp - 在 Cakephp 中保存与同一模型相关的多个记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30287600/

相关文章:

mysql - CakePHP 分页器中的数字排序

php - 使用 composer 在 cakephp 3 中安装 Facebook-php-ads-sdk

android - 在图库中保存图像

cakephp - 调整 AppModel 中的 beforeFind()

cakephp-2.0 - 使用cakePHP的Hash类从数组中提取数据

php - 有效管理数据变更

javascript - 如何保存 HTML 中的更改?

java - 将更改(永久)保存在数组列表中?

php - 复杂分页与 NOT IN 条件 cakephp 2

mysql - 如何使用mysql数据库在cakephp的选择列表中显示货币符号