duangou2028 2012-09-12 13:39
浏览 29

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

I have a cakephp2 application where users login based on their credentials on a db. Authentication & Authorizations work fine.

I have a notes table, in which users's notes are stored. Each user can have many notes.

On one page I need to extract all the notes of the logged in user.

I have created a note model :

class Note extends AppModel {

public $name = 'Note';

public $belongsTo = 'User';

}

and in my user model I have:

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

In the usercontroller i have created:

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

but no matter what find statement I run, I can't get only the notes for the specific user. I run debug($id) and I get false on screen, so obviously the current user id is not being recognized.

I have to list all the notes of a user then link to each individual notes page.

  • 写回答

1条回答 默认 最新

  • douguiyan9164 2012-09-24 13:21
    关注

    Integrate an AUTH component in cakePHP ...so you will get logged in users ID/Info by auth functions and array like($this->Auth->user('id'))

    for auth integration use below link as refence http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

    now use this user ID like this in finding records

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

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程