0

我正在使用cross-joinDoctrine_RawSql 加载 3 个不同的表。这让我回到了以下对象:

User              -> User class (doctrine base class)
    Settings      -> DoctrineCollection of Setting
    User_Settings -> DoctrineCollection of User_Setting

上面的对象是一个many-to-many关系的结果UserSettingwhereUser_Setting作为一个引用表User_Setting还包含另一个名为value. 这显然包含了对应的值Setting

到目前为止一切都很好,但是返回对象的SettingsUser_Settings 属性绝不会相互关联(当然除了字段)。Usersetting_id

有没有直接从Settings属性遍历到对应User_Settings属性的直接方法?

这是相应的查询:

        $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();

编辑:

1:这是相关的 YAML 标记

//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. 这是目标代码(从 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'));
4

2 回答 2

0

您可以在 User_Setting 类中定义缺失的关系:

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',
        ));
    }
}
于 2010-04-21T09:04:24.357 回答
0

实际上,您应该在 ref 表中定义一对多关系,但使用 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'));
  }
}

然后你将有一个 Doctrine_Record 而不是一个 Doctrine_Collection。

于 2010-04-21T12:33:29.443 回答