How to test if a doctrine records has any relations that are used
Posted
by murze
on Stack Overflow
See other posts from Stack Overflow
or by murze
Published on 2010-05-20T07:33:38Z
Indexed on
2010/05/20
9:10 UTC
Read the original article
Hit count: 274
Hi,
I'm using a doctrine table that has several optional relations (of types Doctrine_Relation_Association
and Doctrine_Relation_ForeignKey
) with other tables.
How can I test if a record from that table has connections with records from the related table.
Here is an example to make my question more clear. Assume that you have a User and a user has a many to many relation with Usergroups and a User can have one Userrole How can I test if a give user is part of any Usergroups or has a role.
The solution starts I believe with
$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
I'm looking for a general solution that will work no matter how many relations there are between user and other tables.
Thanks!
© Stack Overflow or respective owner