我希望通过在关联表中添加一些额外的字段来在多对多关系中保留一些额外的数据。例如,我想跟踪用户在网络中的角色,例如“会员”,“主持人”,“管理员”等。我还想跟踪他/她何时加入网络。现在,我正在寻找的是使用Doctrine检索这些额外字段的有效方法。典型但非常简化的DQL查询可能如下所示:
// find.network.by.slug
Doctrine_Query::create()
->select('*')
->from('Network n')
->leftJoin('n.Members u')
->where('n.slug = ?');
所以,鉴于我有一个来自会员关系的用户,有什么方法可以“回溯”到NetworkMembers关系并获取额外的字段?
< / LI>如果没有,我如何重新安排我的架构以使我能够有效地执行此操作?
(即我不想遍历所有网络,成员必须找到我开始的网络并获取角色和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
答案 0 :(得分:3)
我自己得到了它,只需采取一种略微不同的方法:
// find.network.by.slug
Doctrine_Query::create()
->select('*')
->from('Network n')
->leftJoin('n.NetworkMembers nm')
->leftJoin('nm.Member u')
->leftJoin('n.Founder f')
->leftJoin('n.Icon icon')
->where('n.slug = ?')
// later:
$members = $this->network->NetworkMembers;
foreach($members as $networkMember) {
echo $networkMember->Member->username;
echo $networkMember->role; // Here I can access the
// NetworkMember association directly
}