我目前正在尝试允许用户对已经完成的事件进行存档。
然后,将在存档表中查看该事件。
因此,基本上我有一个存档表和一个事件表,当用户想要存档事件时,他们应该能够在存档添加表单中查看事件(需要使用事件的$id填充)。
但是我不知道如何填充这个字段..我已尝试设置一个值..但事件不是会话,所以不起作用,我也尝试在表单开始时设置$id,但也不起作用。
下面是events控制器中我的存档函数的代码。
public function archive($id = null) {
if ($this->request->is('post')) {
$event = $this->Event->read($id);
$archive['Archive'] = $event['Event'];
$archive['Archive']['eventID'] = $archive['Archive']['archiveID'];
unset($archive['Archive']['archiveID']);
$this->loadModel('Archive');
$this->Archive->create();
if ($this->Archive->save($archive)) {
$this->Session->setFlash(__('The event has been archived'));
$this->Event->delete($id);
$this->redirect(array('action' => 'eventmanage'));
} else {
$this->Session->setFlash(__('The event could not be archived. Please, contact the administrator.'));
}
}
}发布于 2013-01-22 02:21:31
您需要执行以下操作之一:
在控制器中使用 $this->request->data 设置字段的值。
public function add($id = null) {
if ($this->request->is('post')) {
[..snip..]
}
$this->loadModel('Event');
$event = $this->Event->read($id);
$this->request->data['Archive'] = $event['Event'];
}或
更新表单以设置值。
使用相同的事件更新现有代码:
public function add($id = null) {
if ($this->request->is('post')) {
[..snip..]
}
$this->loadModel('Event');
$this->set('event', $this->Event->read($id));
}然后在您的表单中的Archives/add.ctp文件中,更新每个输入以反映$event的值。
echo $this->Form->input('eventID', array('type' => 'hidden', 'value' => $event['Event']['id']));或
编写一个移动记录的函数。
在事件视图上放置一个名为“Archive”的按钮。在事件控制器中创建一个将对事件进行存档的方法。
public function archive($id = null) {
if ($this->request->is('post')) {
$event = $this->Event->read($id);
$archive['Archive'] = $event['Event'];
$archive['Archive']['event_id'] = $archive['Archive']['id'];
unset($archive['Archive']['id']);
$this->loadModel('Archive');
$this->Archive->create();
if ($this->Archive->save($archive)) {
$this->Session->setFlash(__('The event has been archived'));
$this->Event->delete($id);
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The event could not be archived. Please, contact the administrator.'));
}
}
}https://stackoverflow.com/questions/14444389
复制相似问题