CakePhp从另一个表中为特定用户提取数据


CakePhp extract data from another table for specific user

我有一个cakehp2应用程序,用户可以根据他们在数据库上的凭据登录。身份验证&授权运作良好。

我有一个笔记表,其中存储了用户的笔记。每个用户可以有许多笔记。

在一个页面上,我需要提取登录用户的所有笔记。

我创建了一个笔记模型:

class Note extends AppModel {
public $name = 'Note';
public $belongsTo = 'User';
}

在我的用户模型中,我有:

class User extends AppModel {
public $name = 'User';
public $displayField = 'name';
public $hasMany = 'Note';....

在我创建的用户控制器中:

public function getnotes($id = null) {
$this->User->id = $id;
}

但无论我运行什么find语句,我都不能只获得特定用户的注释。我运行debug($id),在屏幕上得到false,所以很明显当前用户id没有被识别。

我必须列出用户的所有笔记,然后链接到每个单独的笔记页面。

在cakeHP中集成AUTH组件。。。因此,您将通过auth函数和类似数组($this->auth->user('ID'))获得登录用户的ID/Info

对于auth集成,请使用以下链接作为参考http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

现在在查找记录时使用此用户ID

$this->Note->find(‘all’,array(‘conditions’=>array(’Note.user_id’=>$this->Auth->user(‘id’))