开发者

belongsTo relationship & filtering in 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' =&g开发者_如何学Got; '',
        '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' => ''
    )
);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜