【问题标题】:Cakephp retrieving id from another viewCakephp 从另一个视图中检索 id
【发布时间】:2012-08-16 02:16:06
【问题描述】:

尝试创建一个表单和一个确认页面,要求确认用户是否想与用户建立关系。该表单只有一个字段,该字段采用 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'));


    ?>

【问题讨论】:

    标签: mysql forms cakephp views identifier


    【解决方案1】:

    将 ID 添加到重定向:

    $this->redirect(array('controller' => 'Relationships', 'action' => 'confirm'));
    

    变成这样:

    $this->redirect(array('controller' => 'Relationships', 'action' => 'confirm', $this->request->data['Relationship']['receiver_id']));
    

    【讨论】:

    • 谢谢,但还是不行。我使用参数对吗?还是我应该使用参数?
    • 这是一种正确的做法;尽管您可能想通过$account_id 而不是receiver_id,但您肯定在其他地方遇到了问题。
    • @Predominant 应该是$this-&gt;request-&gt;data['Relationship']['receiver_id']
    【解决方案2】:

    您可以直接访问 $id,而无需使用 $this->request->params。您可以在您的 confirm() 方法中使用它。

    【讨论】:

    • Nah 也不起作用。 $id 和 $compName 都返回空数组。我想我无法从数据库中获取价值。我将如何处理两者? $compName 应该从 Account 表中获取 company_name。 $id 是关系表中的 receiver_id。
    猜你喜欢
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 2012-07-02
    • 2012-04-03
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多