我的表格user
包含字段username
,password
和type
。 type
可以是以下任何一种或其组合:员工,供应商和客户端,例如用户可以是供应商和客户端,也可以是其他组合。对于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;
}
此代码将多个值保存为数据库中的逗号分隔值。
当我正在编辑用户时,主要问题出现了。如果用户在创建用户期间选择了多种类型,则不会检查所有类型的复选框。
答案 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并且数组已正确排列,复选框仍未选中