我正在使用一个与其他表有几个可选关系(类型Doctrine_Relation_Association
和Doctrine_Relation_ForeignKey
)的学说表。如何测试该表中的记录是否与相关表中的记录有连接。
这是一个让我的问题更清楚的例子。假设您有一个用户,并且一个用户与用户组有多对多关系,并且一个用户可以有一个用户角色我如何测试给定用户是否是任何用户组的一部分或具有角色。
我相信解决方案始于
$relations = Doctrine_Core::getTable('User')->getRelations();
$user = Doctrine_Core::getTable('User')->findOne(1);
foreach($relations as $relation) {
//here should go a test if the user has a related record for this relation
if ($relation instanceof Doctrine_Relation_Association) {
//here the related table probably has more then one foreign key (ex. user_id and group_id)
}
if ($relation instanceof Doctrine_Relation_ForeignKey) {
//here the related table probably has the primary key of this table (id) as a foreign key (user_id)
}
}
//true or false
echo $result
我正在寻找一个通用的解决方案,无论用户和其他表之间有多少关系,它都能正常工作。
谢谢!