Function cakephp数据检索

Function cakephp数据检索,function,cakephp,information-retrieval,Function,Cakephp,Information Retrieval,我正试图创建一个页面,将吐出一个用户发票。我在检索特定用户的发票时遇到问题。问题在于控制器中的函数和设置$id。我不确定如何将其设置为当前用户 这是我函数中的相关代码 public function payInvoice(){ $this->set('title_for_layout', 'Pay Invoice'); $this->set('stylesheet_used', 'homestyle'); $this->set('image_used'

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

这是我函数中的相关代码

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));
}
这就是答案 包括我的观点:

<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>

非常感谢您提供的任何帮助

为什么不将会话id及其值指定为用户id?

对于当前用户,您需要会话,我假设会话包含用户id。 试试这个

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));
}

当用户成功登录时,您在会话中设置他的id 像

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

在你的控制器里的任何地方

   $id = $this->Session->read('Myid');

你不需要添加到应用程序控制器文件中

我会在应用程序控制器中这样做吗?不需要。只要按照@cutebabs在她的回答中所说的去做就行了。那么我会把第一行代码放在用户控制器的登录函数中吗?然后将第二行代码放入发票控制器?在您编写的登录函数中,我认为userscontroller我在userscontroller中编写了登录函数,我将在最初的postfirst写入会话写入中发布它,然后重定向。。对于$myid,为username=$this->data['User']['txtusername']中的users表中的fetch id编写select查询;用户id是登录用户的id吗?no userid是登录用户的id登录用户的id在$this->Auth->user('id')中;tigrang,我要在其他地方添加代码吗?我已经筋疲力尽了,对此我非常恼火。你想通过哪个值?解决方案是什么?请分享
   $this->Session->write('Myid',$myid);
   $id = $this->Session->read('Myid');