我希望通过在关联表中添加一些额外的字段来在多对多关系中保留一些额外的数据。例如,我想跟踪用户在网络中的角色,例如“成员”、“版主”、“管理员”等。我还想跟踪他/她何时加入网络。现在,我正在寻找一种使用 Doctrine 检索这些额外字段的有效方法。一个典型的但非常简化的 DQL 查询可能如下所示:
// find.network.by.slug
Doctrine_Query::create()
->select('*')
->from('Network n')
->leftJoin('n.Members u')
->where('n.slug = ?');
那么,鉴于我有一个来自 Members 关系的 User,有什么方法可以“回溯”到 NetworkMembers 关系并获取额外的字段?
如果没有,我如何重新安排我的架构以使我能够有效地做到这一点?
(即我不想遍历成员必须找到我开始的网络并获取角色和 member_since 字段的所有网络)。
我的问题的代码说明:
$networkTable = Doctrine::getTable('Network');
$network = $networkTable->executeOne('find.network.by.slug', $slug);
$members = $network->Members;
foreach($members as $member) {
// How do I access the fields in the NetworksMember association?
}
架构如下:
User:
tableName: users
columns:
user_id:
name: user_id as userId
type: integer(8)
unsigned: 1
primary: true
autoincrement: true
username:
type: string(30)
notnull: true
unique: true
relations:
Networks:
class: Network
local: user_id
foreign: network_id
refClass: NetworkMembers
type: many
Network:
tableName: networks
actAs:
Sluggable:
unique: true
fields: [name]
canUpdate: true
Timestampable:
columns:
id:
type: integer(8)
unsigned: 1
primary: true
autoincrement: true
name:
type: string(64)
notnull: true
relations:
Members:
class: User
type: many
refClass: NetworkMembers
local: network_id
foreign: user_id
foreignAlias: Networks
foreignType: many
NetworkMembers:
actAs:
Timestampable:
created:
name: member_since as memberSince
type: timestamp
updated:
disabled: true
columns:
networkId:
name: network_id as networkId
type: integer(8)
unsigned: 1
primary: true
userId:
name: user_id as userId
type: integer(8)
unsigned: 1
primary: true
role:
type: enum
values: [member, moderator, admin]
default: member
relations:
Network:
type: one
local: network_id
foreign: id
Member:
class: User
type: one
local: user_id
foreign: user_id