如何使用CakePHP中的一个表单保存多个数据实例

时间:2012-12-21 12:53:20

标签: cakephp cakephp-2.0 html-form

我已经构建了一个管理员可以添加和编辑用户的屏幕。我可以毫无问题地添加用户,但是当我测试用户的编辑方式时,我注意到如果我有多个用户,我只能编辑列出的最后一个用户。我无法编辑任何其他用户。

这是我的代码:

<?php foreach ($personel as $person) { ?>
<div id="edituser<?php echo $person['Personel']['id']; ?>" class="modal" style="display:none;">
    <?php
        $edituserformname = "editUser" + $person['Personel']['id'];
    ?>
    <div class="modal-header">
        <a class="close" data-dismiss="modal">×</a>
        <h3>Edit User - <?php echo $person['Personel']['firstname']; ?> <?php echo $person['Personel']['surname']; ?></h3>
    </div>
    <div class="modal-body iframed">
        <?php echo $
        <?php 
            echo $this->Form->create('Personel', array(
                'class' => 'form-horizontal',
                'id' => $edituserformname
            ));
            echo $this->Form->input('id', array(
                'type' => 'hidden',
                'value' => $person['Personel']['id']
            ));
            echo $this->Form->input('firstname', array(
                'type' => 'text',
                'label' => 'First Name',
                'class' => 'span5',
                'value' => $person['Personel']['firstname']
            ));
            echo $this->Form->input('surname', array(
                'type' => 'text',
                'label' => 'Surname',
                'class' => 'span5',
                'value' => $person['Personel']['surname']
            ));
            echo $this->Form->input('email', array(
                'type' => 'text',
                'label' => 'E-Mail',
                'class' => 'span5',
                'value' => $person['Personel']['email']
            ));
            echo $this->Form->input('companyid', array(
                'type' => 'hidden',
                'value' => $company['Company']['id']
            ));
            echo $this->Form->input('accesslevel', array(
                'label' => 'Access Level',
                'options' => $roles,
                'empty' => 'Select Access Level',
                'class' => 'span5',
                'value' => $person['Personel']['accesslevel']
            ));
            $pocval = array('1' => 'Yes', '0' => 'No');
            echo $this->Form->input('poc', array(
                'label' => 'Point of Contact?',
                'options' => $pocval, 
                'value' => $person['Personel']['poc']
            ));
            echo $this->Form->input('password', array(
                'type' => 'text',
                'label' => 'Password',
                'class' => 'span5',
                'value' => $company['Personel']['password']
            ));
            echo $this->Form->input('telephone', array(
                'type' => 'text',
                'label' => 'Telephone',
                'class' => 'span5',
                'value' => $company['Personel']['telephone']
            ));
            echo $this->Form->input('type', array(
                'type' => 'hidden',
                'value' => '0'
            ));
         ?>
     </div>
    <div class="modal-footer">
        <?php
        echo $this->Form->submit('Save & Close', array(
                'type' => 'submit',
                'class' => 'btn btn-primary',
                'id' => 'editusermodal'
            ));
        echo $this->Form->end();
         ?>
    </div>
</div>
<?php } ?>

如何解决此问题?我对使用iFrame有一个想法,但我不愿意使用这种方法。我宁愿能够通过CakePHP来做到这一点。

非常感谢

2 个答案:

答案 0 :(得分:0)

您不需要使用iFrame。它不起作用,因为您的表单没有正确关闭。

$this->Form->create()$this->Form->end()应该在div之外。

尝试以下方法:

<?php
    echo $this->Form->create('Personel', array(
        'class' => 'form-horizontal',
        'id' => $edituserformname
    ));
?>
    <div class="modal-body iframed">
        <?php 
            echo $this->Form->input('id', array(
                'type' => 'hidden',
                'value' => $person['Personel']['id']
            ));
            echo $this->Form->input('firstname', array(
                'type' => 'text',
                'label' => 'First Name',
                'class' => 'span5',
                'value' => $person['Personel']['firstname']
            ));
            echo $this->Form->input('surname', array(
                'type' => 'text',
                'label' => 'Surname',
                'class' => 'span5',
                'value' => $person['Personel']['surname']
            ));
            echo $this->Form->input('email', array(
                'type' => 'text',
                'label' => 'E-Mail',
                'class' => 'span5',
                'value' => $person['Personel']['email']
            ));
            echo $this->Form->input('companyid', array(
                'type' => 'hidden',
                'value' => $company['Company']['id']
            ));
            echo $this->Form->input('accesslevel', array(
                'label' => 'Access Level',
                'options' => $roles,
                'empty' => 'Select Access Level',
                'class' => 'span5',
                'value' => $person['Personel']['accesslevel']
            ));
            $pocval = array('1' => 'Yes', '0' => 'No');
            echo $this->Form->input('poc', array(
                'label' => 'Point of Contact?',
                'options' => $pocval, 
                'value' => $person['Personel']['poc']
            ));
            echo $this->Form->input('password', array(
                'type' => 'text',
                'label' => 'Password',
                'class' => 'span5',
                'value' => $company['Personel']['password']
            ));
            echo $this->Form->input('telephone', array(
                'type' => 'text',
                'label' => 'Telephone',
                'class' => 'span5',
                'value' => $company['Personel']['telephone']
            ));
            echo $this->Form->input('type', array(
                'type' => 'hidden',
                'value' => '0'
            ));
         ?>
     </div>
    <div class="modal-footer">
        <?php
            echo $this->Form->submit('Save & Close', array(
                    'type' => 'submit',
                    'class' => 'btn btn-primary',
                    'id' => 'editusermodal'
                ));
             ?>
    </div>
<?php 
    echo $this->Form->end(); 
?>

答案 1 :(得分:0)

如果您希望一次编辑多个记录,可以使用以下

$this->Form->input('ModelName.n.field_name', $options);

所以他们所有人都在循环中运行它:

echo $this->Form->create(); // only one start
foreach($users as $k => $user) {
    echo $this->Form->id(sprintf('Personel.%s.id', $k), array(
         'value' => $user['Personel']['id']
    ));
    echo $this->Form->input(sprintf('Personel.%s.field', $k), array(
         'value' => $user['Personel']['field']
    ));

    // etc
}
echo $this->Form->end(); // only one end

要保存所有内容,您可以使用saveAll()

$this->Personel->saveAll($this->data['Personel']);