首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cakephp数据检索

cakephp数据检索
EN

Stack Overflow用户
提问于 2012-05-24 12:06:16
回答 3查看 82关注 0票数 0

我试图创建一个页面,将吐出一个用户的发票。我在检索特定用户的发票时遇到了问题。控制器中的功能和设置$id有问题。我不确定如何将其设置为当前用户。

下面是我的函数中的相关代码

代码语言:javascript
复制
public function payInvoice(){

    $this->set('title_for_layout', 'Pay Invoice');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.jpg');   
    $this->layout='home_layout';
    $userid = $this->Auth->user('id');
    $this->set('Invoices', $this->Invoice->findByBiller($userid));
}

这是我的观点包含的答案:

代码语言:javascript
复制
<table width="100%" border="1">
                <tr>
                    <th>Biller</th>
                    <th>Subject</th>
                    <th>Date</th>
                    <th>Action</th>
                </tr>

                <?php foreach($Invoices as $invoice):?>
                    <tr> 
                        <td align='center'><?php echo $this->Html->link($Invoices['Invoice']['to'], 
                        array('action' => 'viewInvoice', $Invoices['Invoice']['to'])); ;?> </td>
                        <td align='center'><?php echo $Invoices['Invoice']['subject']; ?></td>
                        <td align='center'><?php echo $Invoices['Invoice']['datecreated']; ?></td>
                        <td align='center'><a href="viewinvoice"><button>View Invoice</button></a><a href="disputeinvoice"><button>Dispute Invoice</button></a></td>
                    </tr>
                <?php endforeach; ?>

            </table>

在代码中,我对它进行了硬编码,这样用户15就可以查看他的所有发票,但我不想让我的系统硬编码。我希望将其设置为$id='currentuser',但不确定如何编码。

用户登录功能

代码语言:javascript
复制
 public function login() {


    $this->set('title_for_layout', 'Individual Registration');
    $this->set('stylesheet_used', 'style');
    $this->set('image_used', 'eBOXLogo.jpg');

    if ($this->request->is('post')){
    $this->request->data['User']['password'] = 'qazwsx';
   if ($this->Auth->login()) {

       $this->redirect($this->Auth->redirect('eboxs/home'));
       $this->Session->write('Myid',$myid);
    } 
    else {
        $this->Session->setFlash('Username or password is incorrect');
    }
    }else{
    $this->Session->setFlash('Welcome, please login');
    }


}

任何帮助都将不胜感激

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-24 13:02:36

对于当前用户,您需要拥有会话,假设会话包含user_id。尝尝这个

代码语言:javascript
复制
public function payInvoice(){
        $id=$this->Session->read('user_id');
        $this->set('title_for_layout', 'Pay Invoice');
        $this->set('stylesheet_used', 'homestyle');
        $this->set('image_used', 'eBOXLogoHome.jpg');   
        $this->layout='home_layout';
        $this->set('Invoices', $this->Invoice->findByBiller($id));
}
票数 0
EN

Stack Overflow用户

发布于 2012-05-24 12:10:44

为什么不分配一个带有值的会话id作为user_id呢?

票数 0
EN

Stack Overflow用户

发布于 2012-05-24 13:11:11

当用户成功登录时,U将其id设置在会话中,

代码语言:javascript
复制
   $this->Session->write('Myid',$myid);

当用户名和密码正确时,从数据库获取$myid

并在控制器中的任意位置获取,例如,

代码语言:javascript
复制
   $id = $this->Session->read('Myid');

而且不需要添加到app_controller文件中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10730996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档