У меня есть Пользователи и События, общая таблица 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' => ''
)
);
Создает точные записи в базе данных 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');