我有两个模型,关系为Advisor belongsTo Room
,Room hasMany Advisor
。 Advisor
在数据库(Advisor.room_id
)中有一个指向特定房间的外键约束。此值的默认值为NULL
值(表示没有房间的顾问)。无法在CakePHP中使用复选框保存所有数据
假设我有一个Advisor
,room_id
设置为n
。我现在想从Advisor
取消分配的nth
室 - 使用选择字段,我可以重置room_id
到NULL
,具有以下request->data
结构:
[Room] => Array
(
[name] => TestRoom2
[type] => single suite
[id] => 4
)
[Advisor] => Array
(
[0] => Array
(
[id] => 14
[room_id] =>
[name] => foo
)
)
然而,当我试图通过复选框的使用要做到这一点与生成的$this->request->data
相同,MySQL拒绝更新NULL
的值。
另外,在第二种情况下,似乎在request->data
中明确更改room_id
的值不起作用。但是,如果我将Advisor.0.name
更改为hax
(通过直接修改request->data
)name
字段确实保存。
我保存通过调用$this->Room->saveAll($request->data, array('deep' => true))
- 无论是在选择字段和复选框的情况下。
我通过多次调用Form
帮手产生一系列的复选框的:
$count = 0;
// $key is the Advisor id, and $attributes is an array of the form
// array('name' => (string), 'disabled' => (bool))
foreach($options['advisorList'] as $key => $attributes) {
$form[] = $this->Form->hidden(sprintf('Advisor.%s.id', $count), array('value' => $key));
$form[] = $this->Form->input(sprintf('Advisor.%s.room_id', $count), array(
'type' => 'checkbox',
'label' => $attributes['name'],
'disabled' => $attributes['disabled']));
$count++;
}
此外,如果room_id
已经被设置为NULL
,是没有问题的设置room_id
- 除了,room_id
会不管复选框是否被选中。
任何帮助将不胜感激 - 谢谢!