开发者

HABTM only working one way

I have Users and Events, and a shared table of events_users, and a HABTM between user/event set to this join table.

User schema

id, …, …

Event schema

id,

user_id {this is the Owner of the event}

events_users schema

id

user_id

event_id

user model

public $hasAndBelongsToMany = array(
    'Event' => array(
        'className' => 'Event',
        'joinTa开发者_开发知识库ble' => 'events_users',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'event_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

Event model

var $hasAndBelongsToMany = array(
    …
    …

    'SharedUser' => array(
        'className' => 'User',
        'joinTable' => 'events_users',
        'foreignKey' => 'event_id',
        'associationForeignKey' => 'user_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

It creates the events_users db entries fine using the scaffolded add/edit form, but when I try and pull it, I get ALL events instead of just the ones they have 'access' to through the join table. When I view an Event, it auto-magically shows me associated users using this join table.

Attempts to pull Events a User has 'access' to from users_controller:

// this throws error of Unknown column 'EventUser.user_id' in 'where clause' (no matter what combination of pluralization or capitalization and underscore, ie EventUser, events_users, Events_Users)
$events = $this->User->Event->find('all', array('conditions'=> array('EventUser.user_id'=> $id))); 

// this throws error of Undefined property: UsersController::$EventUser 
$events = $this->User->Event->find('all', array('conditions'=> array('UsersEvents.user_id'=> $id))); 

// I would think this just gives the ones they "own", i have a user_id column in Event for the creator of event, but it returns empty
$events = $this->User->Event->find('all', array('conditions'=> array('user_id'=> $id)));  


// This gives me all events
$events = $this->User->Event->find('all');


$user = $this->User->find('first', array('conditions' => array('User.id' => $id));
debug($user['Event']);

Possibly contain only the Event model, if there's more stuff attached to the User model you don't want.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜