スタッフのシフト管理のための編集画面を作成しています。
親子関係にあるテーブル(親:work_shifts、子:work_shift_details)の情報を表示して、
子レコードだけ編集させ、「保存」をすると
・既存であれば更新
・新規行であれば追加
したいのですが、
実際に保存をすると親子を結合しているキーがAutoIncrementされた新規IDに成り代わってしまい
うまくいきません。
[保存前]
親テーブル(work_shifts)
id | event_id | memo | ... |
---|---|---|---|
12 | 555 | NULL | ... |
... | ... | ... | ... |
820 | ... | ... | ... |
(↑最大IDが820とする) |
子テーブル(work_shift_details)
id | work_shift_id | staff_id | ... |
---|---|---|---|
21 | 12 | 18 | ... |
22 | 12 | 35 | ... |
23 | 12 | 45 | ... |
↓↓↓
[保存後]
親テーブル(work_shifts)
id | event_id | memo | ... |
---|---|---|---|
12 | 555 | NULL | ... |
... | ... | ... | ... |
820 | ... | ... | ... |
821 | ... | ... | ... |
子テーブル(work_shift_details)
id | work_shift_id | staff_id | ... |
---|---|---|---|
21 | 821 | 18 | ... |
22 | 821 | 35 | ... |
23 | 821 | 45 | ... |
※work_shifts.id = work_shift_details.work_shift_id
基本的な作りが間違っているのでしょうか。それとも少しのミスなのでしょうか。
ご指導のほどお願いします。
Model
<?php App::uses('AppModel', 'Model'); class WorkShift extends AppModel { public $hasMany = "WorkShiftDetail"; public $belongsTo = array( 'Event' => array( 'className' => 'Event', 'foreignKey' => 'event_id', 'fields' => array( 'name', 'event_date', // 省略 ) ) ); ?>
Controller
<?php App::uses('AppController','Controller'); class WorkShiftsController extends AppController { /* 一部省略 */ public function edit($eventId = null) { if ($eventId != null) { // 編集画面に最初にアクセスしたとき $this->set('title_for_layout', 'シフト編集'); $workShifts = $this->WorkShift->findByEventId($eventId); $event = $workShifts['Event']; $ownerBox = $this->OwnerBox->findById($event['owner_box_id'])['OwnerBox']; $roles = $this->Role->find('list'); $this->set('work_shifts', $workShifts); $this->set('roles', $roles); $staffsInOwnerBox = $this->Staff->find('list', array( 'conditions' => array('Staff.owner_box_id' => $event['owner_box_id']), )); $staffsAll = $this->Staff->find('list', array( 'fields' => array('id', 'name', 'area_id'), 'order' => array('Staff.area_id') )); $staffs = $staffsInOwnerBox + array_values($staffsAll)[0]; $this->set('staffs', $staffs); } else { // 「保存」を押したとき $this->WorkShift->saveAll($this->data); // レンダリングは未実装のため省略 } } } ?>
View
<div class='work-shifts-edit'> <article> <section class='opened'><?php ?><table> <tr> <th>場所</th> <td colspan='3'><?php echo $owner_box_name; ?></td> </tr> <tr> <th>開催日時</th> <td colspan='3'><?php echo h(str_replace("-","/",$work_shifts['Event']['event_date'])." ".$work_shifts['Event']['display_start_at']." - ".$work_shifts['Event']['display_end_at']); ?></td> </tr> <tr> <th>イベント名</th> <td colspan='3'><?php echo $work_shifts['Event']['name']; ?></td> </tr> <tr> <th>人数</th> <td colspan='3'><?php echo $work_shifts['Event']['people_count']; ?>人</td> </tr> <tr> <th>役割</th> <th>スタッフ名</th> <th>時間</th> <th>確定</th> </tr><?php echo $this->Form->create('WorkShift', array('action' => 'edit', 'class' => 'form-horizontal','novalidate' => true)); foreach($work_shifts['WorkShiftDetail'] as $key => $wsd) { ?><tr> <td><?php // hidden WorkShiftDetail.work_shift_id echo $this->Form->input('WorkShiftDetail.'.$key.'.work_shift_id', array( 'type'=>'hidden', 'value' => $wsd['work_shift_id'])); // hidden WorkShiftDetail.id echo $this->Form->input('WorkShiftDetail.'.$key.'.id', array( 'type'=>'hidden', 'value' => $wsd['id'])); // 役割 echo $this->Form->input('WorkShiftDetail.'.$key.'.role_id', array( 'label' => false, 'options' => array('' => '--', $roles), 'value' => $wsd['role_id'])); ?></td> <td><?php echo $this->Form->input('WorkShiftDetail.'.$key.'.staff_id', array( 'label' => false, 'value' => $wsd['staff_id'])); ?></td> <td><?php echo $this->Form->input('WorkShiftDetail.'.$key.'.start_at', array( 'default' => date('H:i', strtotime($wsd['start_at'])), 'type' => 'time', 'timeFormat' => '24', 'label' => false, 'value' => $wsd['start_at'])); echo $this->Form->input('WorkShiftDetail.'.$key.'.end_at', array( 'default' => date('H:i', strtotime($wsd['end_at'])), 'type' => 'time', 'timeFormat' => '24', 'label' => false, 'value' => $wsd['end_at'])); ?></td> <td><?php echo $this->Form-> input('WorkShift.'.$key.'.fixed_flg', array( 'label' => false, 'value' => $wsd['fixed_flg'])); ?></td> </tr><?php } ?><tr> </table> <?php echo $this->Form->end('保存'); ?>
回答2件
あなたの回答
tips
プレビュー