Doctrine: How to traverse from an entity to another 'linked' entity?
Posted
by ropstah
on Stack Overflow
See other posts from Stack Overflow
or by ropstah
Published on 2010-04-20T20:31:25Z
Indexed on
2010/04/20
20:33 UTC
Read the original article
Hit count: 208
I'm loading 3 different tables using a cross-join
in Doctrine_RawSql. This brings me back the following object:
User -> User class (doctrine base class)
Settings -> DoctrineCollection of Setting
User_Settings -> DoctrineCollection of User_Setting
The object above is the result of a many-to-many
relationship between User
and Setting
where User_Setting
acts as a reference table. User_Setting
also contains another field named value
. This obviously contains the value of the corresponding Setting
.
All good so far, however the Settings
and User_Settings
properties of the returned User
object are in no way linked to each other (apart from the setting_id
field ofcourse).
Is there any direct way to traverse directly from the Settings
property to the corresponding User_Settings
property?
This is the corresponding query:
$sets = new Doctrine_RawSql();
$sets->select('{us.*}, {s.*}, {uset.*}')
->from('(User us CROSS JOIN Setting s) LEFT JOIN User_Setting uset ON us.user_id = uset.user_id AND s.setting_id = uset.setting_id')
->addComponent('us', 'User us')
->addComponent('uset', 'us.User_Setting uset')
->addComponent('s', 'us.Setting s')
->where('s.category_id = ? AND us.usr_auto_key = ?',array(1, 1));
$sets = $sets->execute();
© Stack Overflow or respective owner