尝试创建一个表单和一个确认页面,该页面要求确认用户是否要与用户建立关系。该表单只有一个字段,该字段采用 id 并将用户定向到确认页面。确认页面有两个按钮,当用户点击确认时,确认和拒绝将提交到数据库,如果用户点击拒绝,来自表单的数据将被丢弃。确认页面还将打印他们尝试添加的用户的用户名。
我似乎无法从添加页面 (add_admin.ctp) 检索 id。
如何从原始添加页面 (add_admin.ctp) 访问 id 以在确认页面 (confirm.ctp) 中使用它?
public function add_admin() {
$this->set('title_for_layout', 'Send A Relationship Request');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
$account_id=$this->User->find('list', array(
'fields'=>array('account_id'),'conditions' => array(
'id' => $this->Auth->user('id'))));
if ($this->request->is('post')) {
$this->Relationship->save($this->request->data);
if ($this->Relationship->validates(array('fieldList'=>array('receiver_id','Relationship.accountExists'))))
{
$this->Relationship->save($this->request->data);
$this->Session->setFlash('The relationship has been saved');
$this->redirect(array('controller'=>'Relationships', 'action'=>'confirm'));
} else {
$this->Session->setFlash('The relationship could not be saved. Please, try again.');
}
}
$this->set('account_id', $account_id);
}
public function confirm($id){
$this->set('title_for_layout', 'Relationships');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
//$id = $this->request->data(`Relationship.receiver_id`);
//$id = $this->params('Relationship.id');
$id = $this->params['Relationship']['id'];
$compName = $this->request->data(`Account.company_name`);
$findName=$this->Account->find('list', array(
'fields'=>array('company_name'),'conditions' => array(
'id' => $id)));
debug($id);
pr($compName);
pr($findName);
$this->Relationship->unbindModel(array('hasMany'=>array('Invoice')));
if ($this->request->is('get')) {
$this->request->data = $this->Relationship->read(NULL, $id);
}
else
{
if ($this->Relationship->save($this->request->data)) {
//$this->Session->setFlash('You have successfully confirmed the relationship.');
if($this->request->data['submit'] == "type_1")
{
$this->Session->setFlash('The relationship has been confirmed');
$this->redirect( array('controller' => 'fields','action' => 'add'));
}
if($this->request->data['submit'] == "type_2")
{
$this->Session->setFlash('The relationship was denied');
$this->redirect( array('controller' => 'templates','action' => 'index'));
}
} else {
$this->Session->setFlash('Unable to update your post.');
}
}
$this->set('compName', $compName);
$this->set('findName', $findName);
$this->set('id', $id);
}
add_admin.ctp
<?php
echo $this->Form->create('Relationship', array('action'=>'add_admin'));
echo $this->Form->hidden('sender_id',array('label'=>'Enter your eBox ID: ', 'type'=>'text', 'default'=>$account_id));
echo $this->Form->input('receiver_id',array('label'=>'Receiver eBox ID: ', 'type'=>'text'));
echo "<br />";
echo $this->Form->end('Submit');
?>
确认.ctp
Do you want to create a relationship with <?php echo $findName ?>
<?php
echo $this->Form->create('Relationship', array('action'=>'confirm'));
echo $this->Form->input('id', array('type'=>'hidden'));
echo $this->Form->button('Confirm', array('name' => 'submit', 'value' => 'type_1'));
echo $this->Form->button('Deny', array('name' => 'submit', 'value' => 'type_2'));
?>