HABTM 仅以一种方式工作
我有用户和事件,以及 events_users 的共享表,以及设置为此连接表的用户/事件之间的 HABTM。
用户架构id,…,…
事件架构id,
user_id {这是事件的所有者}
events_users 架构id
user_id
event_id
用户模型
public $hasAndBelongsToMany = array(
'Event' => array(
'className' => 'Event',
'joinTable' => 'events_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'event_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
事件模型
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' => ''
)
);
它使用脚手架添加/编辑表单很好地创建了 events_users 数据库条目,但是当我尝试拉动它,我得到所有事件,而不仅仅是他们通过连接表“访问”的事件。当我查看事件时,它会神奇地自动向我显示使用此连接表的关联用户。
尝试从 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');
I have Users and Events, and a shared table of events_users, and a HABTM between user/event set to this join table.
User schemaid, …, …
Event schemaid,
user_id {this is the Owner of the event}
events_users schemaid
user_id
event_id
user model
public $hasAndBelongsToMany = array(
'Event' => array(
'className' => 'Event',
'joinTable' => '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');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能
包含
仅事件
模型,如果用户模型附加了更多您不想要的东西。Possibly
contain
only theEvent
model, if there's more stuff attached to the User model you don't want.