duanji1026 2011-07-06 16:21
浏览 30

属于cakePHP中的关系和过滤

I'm a newbie to cakePHP and I've taken on creating a very small "status reporting" project, that would allow a user to report their current status on a project that they were assigned on.

I'm currently using the acl, auth, and session components to allow for multiple tier users to manage one another with an admin creating users, projects, and assigning them to one another. I have also fixed it so that when a user is logged in and goes to add a "status" that their login session automatically takes care of the "user_id" for the status:

$this->data['Status']['user_id'] = $this->Auth->user('id');

What I need help doing now is filter the options for the project that the user can pick to add a "status" to.

I currently have a setup with a table that holds the relationships between users and projects named *projects_users* with *id, project_id, user_id* as fields. But, after baking this setup, when the user is adding a "status" the drop down menu provides all projects rather than strictly the ones they are "assigned to."

I would like to filter the user's options to the projects that they are assigned to. Is there considerable more relationships I should be setting up or is this a pretty easy little function to write? Any help would be greatly appreciated and if I have left out information, I'd be glad to post anything else.

Here is the Status Model setup:

class Status extends AppModel {
var $name = 'Status';
//The Associations below have been created with all possible keys, those that are not needed can be removed

var $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Project' => array(
        'className' => 'Project',
        'foreignKey' => 'project_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);
}

My User Model looks like this:

var $belongsTo = array(
    'Group' => array(
        'className' => 'Group',
        'foreignKey' => 'group_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

var $hasMany = array(
    'Status' => array(
        'className' => 'Status',
        'foreignKey' => 'user_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);


var $hasAndBelongsToMany = array(
    'Project' => array(
        'className' => 'Project',
        'joinTable' => 'projects_users',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'project_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);
  • 写回答

1条回答 默认 最新

  • douzheng9221 2011-07-06 16:36
    关注

    In the database, You should have a column called project_id in the user table that is associated with the project table's id column.

    Then, in your User model, you should create a hasMany relationship with the Project model.

    class User extends AppModel {
        var $name = 'User';
        var $hasMany = 'Project';
    }
    

    Now, when you fetch a particular user, you will get all projects that the user is associated with.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP