编辑cakephp中的用户记录时未选中复选框

时间:2011-02-08 09:04:41

标签: php cakephp checkbox

我的表格user包含字段usernamepasswordtypetype可以是以下任何一种或其组合:员工供应商客户端,例如用户可以是供应商客户端,也可以是其他组合。对于type字段,我使用了多个复选框(请参阅下面的代码)。这是views / users / add.ctp文件:

<div class="users form">
<?php echo $this->Form->create('User');?>
    <fieldset>
        <legend><?php __('Add User'); ?></legend>
    <?php
        echo $this->Form->input('username');
        echo $this->Form->input('password');

        echo $this->Form->input('type', array('type' => 'select', 'multiple' => 'checkbox','options' => array(
     'client' => 'Client',
     'vendor' => 'Vendor',
     'employee' => 'Employee'
   )
  ));
    ?>

    </fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>

在模型文件中,我使用了beforeSave回调方法:

app/models/user.php

function beforeSave() {
       if(!empty($this->data['User']['type'])) {
            $this->data['User']['type'] = join(',', $this->data['User']['type']);
        }
       return true;
  }

此代码将多个值保存为数据库中的逗号分隔值。

当我正在编辑用户时,主要问题出现了。如果用户在创建用户期间选择了多种类型,则不会检查所有类型的复选框。

2 个答案:

答案 0 :(得分:0)

你在beforeSave()中有$this->data['User']['type'] = join(',', $this->data['User']['type']);所以你需要在afterFind()$this->data['User']['type'] = explode(',', $user['User']['type']);中做同样的事情,这会使它再次成为一个数组。

然而,这是一个可怕的设计,你应该考虑正确地做到这一点。

答案 1 :(得分:0)

在跟随此$this->data['User']['type'] = explode(',', $user['User']['type']);之后

@ dogmatic69并且数组已正确排列,复选框仍未选中