views:

104

answers:

2

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.user_id = ?',array(1, 1));

        $sets = $sets->execute();

Edit:

1: this is the related YAML markup

//User relations:
Setting:
  class: Setting
  foreignAlias: User
  refClass: User_Setting
  local: user_id
  foreign: setting_id

//Setting relations:
User:
  class: User
  foreignAlias: Setting
  refClass: User_Setting
  local: setting_id
  foreign: user_id

//User_Setting relations:
Setting:
  foreignAlias: User_Setting
  local: setting_id
  foreign: setting_id
User:
  foreignAlias: User_Setting
  local: user_id
  foreign: user_id  

2. This is the object code (which is generated from YAML):

//BaseUser setup()
    $this->hasMany('Setting', array(
         'refClass' => 'User_Setting',
         'local' => 'user_id',
         'foreign' => 'setting_id'));
    $this->hasMany('User_Setting', array(
         'local' => 'user_id',
         'foreign' => 'user_id'));

//BaseSetting setup()
    $this->hasMany('User', array(
         'refClass' => 'User_Setting',
         'local' => 'setting_id',
         'foreign' => 'user_id'));
    $this->hasMany('User_Setting', array(
         'local' => 'setting_id',
         'foreign' => 'setting_id'));

//BaseUser_Setting setup()
    $this->hasOne('Setting', array(
         'local' => 'setting_id',
         'foreign' => 'setting_id'));
    $this->hasOne('User', array(
         'local' => 'user_id',
         'foreign' => 'user_id'));
A: 

You can define missing relations in User_Setting class:

class User_Setting
{
    // ...
    public function setUp()
    {
        $this->hasMany('Users', array(
            'class' => 'User',
            'local' => 'user_id',
            'foreign' => 'id',
        ));
        $this->hasMany('Settings', array(
            'class' => 'Setting',
            'local' => 'setting_id',
            'foreign' => 'id',
        ));
    }
}
Vladimir
Those are both defined. But if I do this: $sets[0]->Settings[0]->User_Setting << it returns a Doctrine_Collection containing ALL User_Setting related to Setting instead of the corresponding User_Setting related to both Setting AND User. (User is in $sets[0] by the way).
Ropstah
A: 

Actually you should define one-to-many relationship in the ref table, but using hasOne() :

class User_Setting
{
....
  public function setUp()
  {
    parent::setUp();
    $this->hasOne('User', array(
         'local' => 'user_id',
         'foreign' => 'id'));

    $this->hasOne('Setting', array(
         'local' => 'setting_id',
         'foreign' => 'id'));
  }
}

Then you will have a Doctrine_Record instead of a Doctrine_Collection.

DuoSRX
No this is not the case, I read the other answer to quick. I have what is above, please see the updated question...
Ropstah
The relations in your UserSetting are not properly set. It should be "foreign => id" and "local => user_id".
DuoSRX
no i use custom column names. they don't even end with _id, i just wrote it that way for convenience. Mine end with _auto_key. Primary as well as foreign keys. Some foreign keys though have different names more related to the type of relation which those resemble.
Ropstah

related questions