1

通过在 cakephp 2.0 中解决我的 saveAll 问题需要您的帮助。我有 3 个模型:数据表、数据表标题和数据表值。我需要保存所有数据表中最后插入的 ID。我只从 Datasheetsheading 中阅读...

我的控制器:

public function add_datasheet($id = null) {
    $this->set('datasheetsheadings', $this->datasheetsheading->find('all', array('limit' => 0 .','. 9)));
    $this->set('datasheetsheadings2', $this->datasheetsheading->find('all', array('limit' => 9 .','. 9)));
    $this->set('id', $id);
    if ($this->request->is('post')) {
        if ($this->datasheet->save($this->request->data)) {
            $this->datasheetsvalue->saveAll($this->request->data['datasheetsvalue']); 
            $this->Session->setFlash('Your Datasheet >> Lot: ' . $this->request->data['datasheetsvalue'][1]['value'] . ' << has been saved.');
            $this->redirect(array('action' => 'Datasheets/' . $this->request->data['datasheet']['id_tbl_articles']));
        } else {
            $this->Session->setFlash('Unable to add your datasheet.');
        }
    }
}

我的观点:

<?php echo $this->Form->create('datasheet', array('action' => 'add_datasheet')); ?>
    <fieldset>
        <legend><?php echo __('Neues Datenblatt'); ?></legend>
        <?php
        $value_id = 0;
        echo $this->Form->input('datasheet.comment', array('label' => 'Kommentar')) . '<hr>';
        echo '<div style="width:300px;float:left;">';
        foreach ($datasheetsheadings as $datasheetsheading) :
            $value_id = $value_id +1;
            echo $this->Form->input('datasheet.id_tbl_articles', array('type' => 'hidden', 'value' => $id));
            echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_articles', array('type' => 'hidden', 'value' => $id));
            echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_datasheets', array('type' => 'hidden'));
            echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_datasheetsheadings', array('type' => 'hidden', 'value' => $datasheetsheading['datasheetsheading']['id']));
            echo __('<div style="width:140px;color:#FFF;float:left;">' . $datasheetsheading['datasheetsheading']['heading'] . '</div><div style="width:160px;color:#FFF;float:left;text-align:right;">');
            echo $this->Form->input('datasheetsvalue.' . $value_id . '.print', array('type' => 'checkbox', 'label' => 'Im Datenblatt anzeigen', 'style' => 'color:#FFF;'));
            echo '</div><div style="clear:left;"></div><div style="margin-top:-30px;">';
            echo $this->Form->input('datasheetsvalue.' . $value_id . '.value', array('label' => ''));
            echo '</div>';
        endforeach;
        echo '</div>';
        echo '<div style="width:170px;float:left;"></div>';
        echo '<div style="width:300px;float:left;">';
        foreach ($datasheetsheadings2 as $datasheetsheading2) :
            $value_id = $value_id +1;
            echo $this->Form->input('datasheet.id_tbl_articles', array('type' => 'hidden', 'value' => $id));
            echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_articles', array('type' => 'hidden', 'value' => $id));
            echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_datasheets', array('type' => 'hidden'));
            echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_datasheetsheadings', array('type' => 'hidden', 'value' => $datasheetsheading2['datasheetsheading']['id']));
            echo __('<div style="width:140px;color:#FFF;float:left;">' . $datasheetsheading2['datasheetsheading']['heading'] . '</div><div style="width:160px;color:#FFF;float:left;text-align:right;">');
            echo $this->Form->input('datasheetsvalue.' . $value_id . '.print', array('type' => 'checkbox', 'label' => 'Im Datenblatt anzeigen', 'style' => 'color:#FFF;'));
            echo '</div><div style="clear:left;"></div><div style="margin-top:-30px;">';
            echo $this->Form->input('datasheetsvalue.' . $value_id . '.value', array('label' => ''));
            echo '</div>';
        endforeach;
        echo '</div>';
        echo '<div style="clear:left;"></div>';
        echo $this->Form->end(__('Sichern'));
    ?>
    </fieldset>

通过保存数据表,它存储在数据表表中:id(auto_increment), id_tbl_articles, comment, created, modified. 这完美!在数据表值表中,
我得到值:id(auto_increment)id_tbl_datasheetsheadings、和。只有在我得到。我用, ,等做了很多尝试。将值设置为。什么都没用!!!当我更改为时,我得到正确的值,但只有一个。所以我需要这个功能。 谷歌是你的朋友,发现我不能用. 我可以用什么来解决我的问题? id_tbl_articlesvaluesprintid_tbl_datasheetsNULL$hasOne$hasMany$belongsToid_tbl_datasheets$this->datasheet->getlastid()saveAllsavesaveAll
getLastId()saveAll

每个人都感谢您的回答!安迪。

4

1 回答 1

0

我得到了答案!

public function add_datasheet($id = null) {
    $this->set('datasheetsheadings', $this->datasheetsheading->find('all', array('limit' => 0 .','. 9)));
    $this->set('datasheetsheadings2', $this->datasheetsheading->find('all', array('limit' => 9 .','. 9)));
    $this->set('id', $id);
    if ($this->request->is('post')) {
        if ($this->datasheet->save($this->request->data)) {
            $last_id = $this->datasheet->id;
            $val_id = 0;
            foreach ($this->request->data['datasheetsvalue'] as $data_val_id) {
                $val_id = $val_id + 1;
                $this->request->data['datasheetsvalue'][$val_id]['id_tbl_datasheets'] = $last_id;
            }
            $this->datasheetsvalue->saveMany($this->request->data['datasheetsvalue']);
            $this->Session->setFlash('Your Datasheet >> Lot: ' . $this->request->data['datasheetsvalue'][1]['value'] . ' << has been saved.');
            $this->redirect(array('action' => 'Datasheets/' . $this->request->data['datasheet']['id_tbl_articles']));
        } else {
            $this->Session->setFlash('Unable to add your datasheet.');
        }
    }
}

我现在将使用 saveMany。之前,我使用我的 var $val_id 进行 foreach 并设置 datas[]。现在它完美无缺。
不管怎么说,还是要谢谢你!

于 2012-10-24T17:23:08.150 回答