Cakephp 从另一个视图中检索 id

     2023-02-26     65

关键词:

【中文标题】Cakephp 从另一个视图中检索 id【英文标题】:Cakephp retrieving id from another view 【发布时间】: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'));


    ?>

【问题讨论】:

【参考方案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。

跨 CakePHP 检索和传输用户 ID 的安全方法

】跨CakePHP检索和传输用户ID的安全方法【英文标题】:SafewaytoretrieveandtransferUser-IdacrossCakePHP【发布时间】:2011-07-1006:49:19【问题描述】:我是CakeNoob,所以请接受我对这个问题的诚挚歉意。用户在我的Web应用中添加他们的帐户后... 查看详情

从一个视图中选择数据并使用该数据从另一个视图中进行选择

】从一个视图中选择数据并使用该数据从另一个视图中进行选择【英文标题】:Selectdatafromoneviewandusethattoselectfromanotherview【发布时间】:2011-07-0614:08:32【问题描述】:我有2个数据视图,它们通过ID列相关。我需要从视图A中选择... 查看详情

CakePHP 3“包含”不检索“属于”

】CakePHP3“包含”不检索“属于”【英文标题】:CakePHP3\'contain\'doesn\'tretrieve\'belongsTo\'【发布时间】:2021-12-2722:47:02【问题描述】:一个用户有多个产品。每个产品都有一个品牌。在产品表中,我有一个“brand_id”列。目前我做de... 查看详情

CakePHP:如何使用内部联接从两个表中检索数据?

】CakePHP:如何使用内部联接从两个表中检索数据?【英文标题】:CakePHP:Howtoretrievedatafromtwotablesusinganinnerjoin?【发布时间】:2014-06-0115:22:43【问题描述】:我在数据库中有两张表,一张为user(id,first_name,last_name),另一张为location(id... 查看详情

在 CakePHP 中使用 saveAll() 保存多个 hasMany 数据,而无需在视图中写入 id

】在CakePHP中使用saveAll()保存多个hasMany数据,而无需在视图中写入id【英文标题】:SavemultiplehasManydatawithsaveAll()inCakePHPwithoutwritingid\'sintheview【发布时间】:2011-05-0621:13:37【问题描述】:我终于用saveAll()保存了我的hasMany数据-(一... 查看详情

Laravel:如何使用数据透视表从另一个表中检索信息?

】Laravel:如何使用数据透视表从另一个表中检索信息?【英文标题】:Laravel:Howtoretrieveinformationfromanothertableusingapivottable?【发布时间】:2022-01-1519:42:07【问题描述】:问题是我有表单,我想获取它的所有成员。我创建了一个tablep... 查看详情

PHP SQL - 从另一个表中检索共享值与预期不同

】PHPSQL-从另一个表中检索共享值与预期不同【英文标题】:PHPSQL-Retrievesharedvaluesfromaanothertablenotasexpected【发布时间】:2018-12-1311:46:03【问题描述】:我需要根据另一个表中的值从一个表中检索共享值,但不要显示重复项。我有... 查看详情

如何使用单个查询联合从另一个表中检索到的表列表?

】如何使用单个查询联合从另一个表中检索到的表列表?【英文标题】:HowtoUNIONalistoftablesretrievedfromanothertablewithasinglequery?【发布时间】:2019-07-0105:13:03【问题描述】:我有一个表,其中包含PostgreSQL中的表列表:|id|table||--|------|... 查看详情

从另一个视图控制器执行 FetchRequest

】从另一个视图控制器执行FetchRequest【英文标题】:PerformingaFetchRequestfromanotherviewcontroller【发布时间】:2015-05-3006:19:20【问题描述】:在我的新Swift应用程序中,我使用带有SWRevealViewcontroller的滑动侧边菜单。在后视图控制器上有... 查看详情

一个布局中有两个或多个视图(cakephp)

】一个布局中有两个或多个视图(cakephp)【英文标题】:twoormoreviewsinonelayout(cakephp)【发布时间】:2014-10-0311:21:04【问题描述】:是否可以在cakephp2.x的一个布局中添加两个或多个视图?【问题讨论】:【参考方案1】:您可能需... 查看详情

Xamarin 表单从另一个视图获取数据

】Xamarin表单从另一个视图获取数据【英文标题】:Xamarinformsgetdatafromanotherview【发布时间】:2022-01-1805:23:50【问题描述】:我有2个视图。在主要的一个中,我输入了书架的条形码和书籍的条形码,我将它们放在先前扫描的书架上... 查看详情

Kivy:从另一个类的小部件中检索文本?

】Kivy:从另一个类的小部件中检索文本?【英文标题】:Kivy:RetrievingTextfromWidgetinanotherClass?【发布时间】:2020-09-1510:13:28【问题描述】:我正在尝试从另一个类(此处为“GetInfoFromAnotherClass”)访问一个类(此处为“UserInput”)... 查看详情

Cakephp 3 - 如何在验证过程中检索“表”类中的当前登录用户?

】Cakephp3-如何在验证过程中检索“表”类中的当前登录用户?【英文标题】:Cakephp3-Howtoretrievecurrentloggeduserina\'Table\'classduringvalidationprocess?【发布时间】:2015-08-0308:48:16【问题描述】:我正在为我的网站使用CakePhp3,当我创建或... 查看详情

在一个查询中从另一个类中检索数据

】在一个查询中从另一个类中检索数据【英文标题】:Retrievingdatafromanotherclassinonequery【发布时间】:2015-03-2400:30:59【问题描述】:我构建了一个使用Parse的应用程序。我的应用程序允许用户注册、登录然后发布到解析云数据库。... 查看详情

从另一个网站的 html 表中检索一行?

】从另一个网站的html表中检索一行?【英文标题】:Retrievingasinglelinefromahtmltablefromanotherwebsite?【发布时间】:2014-08-1801:37:20【问题描述】:我正在尝试学习curl的用法,但我还不完全了解它是如何工作的。如何使用curl(或其他函... 查看详情

PHP ORM 最佳实践:从另一个对象类型中检索值:

】PHPORM最佳实践:从另一个对象类型中检索值:【英文标题】:PHPORMBestPractices:Retrievingvaluesfromanotherobjecttype:【发布时间】:2012-05-0516:40:58【问题描述】:我最近开始使用RedBeanORM。我有一个用户bean类型来存储用户的信息,以及一... 查看详情

无法使用 cakePHP requestAction 加载完整视图

】无法使用cakePHPrequestAction加载完整视图【英文标题】:unabletoloadfullviewusingcakePHPrequestAction【发布时间】:2012-01-2806:29:20【问题描述】:我有一个问题。我正在尝试使用cakePHP的requestAction方法来加载视图文件,但它似乎不起作用... 查看详情

从另一个局部视图渲染局部视图

】从另一个局部视图渲染局部视图【英文标题】:Renderpartialviewfromanotherpartialview【发布时间】:2014-05-1120:41:35【问题描述】:我正在尝试使用返回部分的ActionResult控制器呈现部分视图。我需要将用户ID传递给控制器​​。我在其... 查看详情