我有两个模型的关系Advisor belongsTo Room
,Room hasMany Advisor
。在数据库 ( ) 中有一个指向特定房间Advisor
的 foreignKey 约束。Advisor.room_id
其默认值为该NULL
值(表示没有房间的顾问)。
假设我有一个Advisor
,room_id
设置为n
. 我现在希望取消分配nth
房间Advisor
- 使用选择字段,我可以重置room_id
为NULL
,具有以下request->data
结构:
[Room] => Array
(
[name] => TestRoom2
[type] => single suite
[id] => 4
)
[Advisor] => Array
(
[0] => Array
(
[id] => 14
[room_id] =>
[name] => foo
)
)
但是,当我尝试通过使用具有相同 generate 的复选框来执行此操作时$this->request->data
,MySQL 拒绝更新该NULL
值。
此外,似乎在第二种情况下更改room_id
显式 in的值request->data
没有效果。但是,如果我要更改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
无论是否选中复选框,都会设置。
任何帮助将不胜感激 - 谢谢!