HABTM 仅以一种方式工作

发布于 2024-11-19 20:14:05 字数 2438 浏览 6 评论 0原文

我有用户和事件,以及 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 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',
        '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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情魔剑神 2024-11-26 20:14:05
$user = $this->User->find('first', array('conditions' => array('User.id' => $id));
debug($user['Event']);

可能包含事件模型,如果用户模型附加了更多您不想要的东西。

$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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文