2012-09-14 47 views
3

UserController的如何在CakePHP中显示登录用户的记录?

public function profile() 
{ 
    $this->set('profile', $this->User->find('all')); 
} 

profile.ctp

<?php 
echo $profile['User']['name']; // Where user is the model and name is the name field in db. 
?> 

我知道我做错了什么,但我找不到它的任何好的解决方案。

回答

0

您必须进行查询。 您已经登录的用户,如果您有

验证

在你的AppController像$组件:

class AppController extends Controller { 

    public $components = array(
     'Session', 
     'Auth' => array(
      'authenticate' => array(
       'Form' => array(
        'fields' => array('username' => 'email') 
       ) 
      ) 
     ), 
    ); 

在你的控制器现在你有经过验证的用户变量在此模式下:

$this->Session->read('Auth.User.id')); 

你可以做这样的查询到你的数据库中检索登录用户的记录:

this->set('profile', $this->User->find('all', array('recursive' => 1, 
        'conditions' => array('User.id' => $this->Auth->user())))); 
+0

我们将如何在视图中回应它? –

+0

鉴于现在你有记录,试着做一个var_dump($ profile),你可以看到所有的字段。但如果你想尝试:echo $ profile ['User'] ['name'];或echo $ profile [0] ['User'] ['name'];我不知道你的数组是如何取决于你的模型之间的关系, –

+0

它的工作,非常感谢你:) –

0
public function profile() 
{ 
    $this->set('profile', CakeSession::read('Auth.User')); 
} 
0

试试这个:

public function profile() 
{ 

    $user = $this->Auth->user(); 
    $this->set('profile', $this->User->find('all', 
     array('conditions'=>array('User.id'=>$user['id'])))); 
} 
+0

用户已经登录...你不需要再次选择相同的用户.. 。你已经在条件数组中传递了你在会话中的相同数据.. –

0

其实我在我的控制器没有什么是

public function profile() 
{ 
    $id=$this->Session->read('Auth.User.id'); 
    $this->set('profile', $this->User->findByid($id)); 

} 

及其工作:)。谢谢大家清除逻辑。

1
public function profile(){ 
    $this->set('current_user', $this->Auth->user()); 
} 
相关问题