我试图在单击按钮后将一个表中的一行数据插入另一个表。我有一个包含班级信息的表。当用户单击注册时,我需要获取该类的ID并将其插入时间表表中。
到目前为止,我正在尝试从POST数据中获取ID。但是,当我运行调试器时,数组中类的ID为空。
我的注册功能:
public function register(){
$classestimetable = $this->Classestimetable->newEntity();
if ($this->request->is('post')) {
$classestimetable->user_id = $this->Auth->user('id');
$classestimetable->unit_id = $this->request->getData(['id']);
debug($classestimetable);exit;
if ($this->Classestimetable->save($classestimetable)) {
$this->Flash->success(__('The class has been added to your schedule.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The class could not be added. Please, try again.'));
}
}
表格代码:
<tbody>
<?php foreach ($units as $unit): ?>
<tr>
<td hidden><?= h($unit->id) ?></td>
<td><?= h($unit->className) ?></td>
<td><?= h($unit->user->firstName), ' ', h($unit->user->lastName) ?></td>
<td><?= h($unit->classDate) ?></td>
<td><?= h($unit->classTime) ?></td>
<td class="actions">
<?= $this->Form->postLink(__('Register'), ['action' => 'register', $unit->id], ['confirm' => __('Are you sure you want to register for "{0}"?', $unit->className)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
调试输出:
object(App\Model\Entity\Classestimetable) {
'user_id' => (int) 11,
'unit_id' => null,
'[new]' => true,
'[accessible]' => [
'unit_id' => true,
'user_id' => true,
'unit' => true,
'user' => true
],
'[dirty]' => [
'user_id' => true,
'unit_id' => true
],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Classestimetable'
}
工作时,我希望unit_id字段包含用户选择注册的类的ID。
答案 0 :(得分:0)
您正在使用$this->request->getData(['id'])
来获取ID。但是,在您构建的URL中,您尚未命名该参数。可以更改为:
['action' => 'register', 'id' => $unit->id]
或将函数的定义更改为:
public function register($id)
然后使用$classestimetable->unit_id = $id;