我有用户和事件,以及events_users的共享表,以及设置到此连接表的用户/事件之间的HABTM。
用户架构
id, …, …
事件架构
id,
user_id {this is the Owner of the event}
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' => ''
)
);
它使用scaffolded add / edit表单创建了events_users db条目,但是当我尝试拉它时,我得到的是所有事件,而不仅仅是他们通过连接表“访问”的事件。当我查看一个事件时,它会自动神奇地向我显示使用此连接表的关联用户。
尝试拉取用户从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');
答案 0 :(得分:0)
$user = $this->User->find('first', array('conditions' => array('User.id' => $id));
debug($user['Event']);
可能contain
只有Event
模型,如果有更多内容附加到您不想要的用户模型。