我確定有一個非常簡單的解決方案,但解釋複雜,請耐心等待。從相關模型保存ID保存
我有一個註冊表格Registration::add()
它由三部分組成。
- 第一個是
Registration
數據的一些字段。 - 二是從三個相關
HABTM
車型,Role
,Category
和CategoryChild
- 三是在
User
我Registration
模型創建的每個記錄有user_id
複選框,我需要保存生成的User.id
成領域。
我敢肯定,我應該能夠使用模型關係來做到這一點,因爲User
模型保存了User.registration_id
的罰款,但它似乎沒有寫入Registration
模型。
我是否需要在我的add.ctp
視圖中創建另一個字段,以便該字段存在於$this->data->request
陣列中?我寧願這樣做,也不願意與beforeSave()
和afterSave()
陷入困境。
所以我只需要創建
$this->Form->input('Registration.user_id', array('type'=>'hidden'));
我的關係,僅供參考,
Registration hasOne User
Registration hasAndBelongsToMany Category
Registration hasAndBelongsToMany CategoryChild
Registration hasAndBelongsToMany Role
User hasOne Registration
我目前使用saveAll($this->request->data)
救我的所有數據,它的管理,以節省一切,除了Registration.user_id
你的關係指定'Registration hasOne User',但你同時擁有'Registration.user_id'和'User.registration_id',所以有些東西看起來不正確 – icc97
你是指一對一的關係嗎? –
@ icc97你是對的,我一直在這麼久,我完全錯過了它。這將解釋爲什麼我的身份證沒有更新兩次!如果你可以把它作爲答案,我會標記爲已接受。 –