学说:如何从一个实体遍历到另一个“链接”实体实体?

发布于 2024-08-29 18:39:08 字数 2733 浏览 3 评论 0原文

我在 Doctrine_RawSql 中使用交叉联接加载 3 个不同的表。这让我回到以下对象:

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

上面的对象是 UserSetting 之间的多对多关系的结果,其中 User_Setting 充当参考表User_Setting 还包含另一个名为 value 的字段。这显然包含了相应Setting的值。

到目前为止一切都很好,但是返回的 User 对象的 SettingsUser_Settings properties 不存在。彼此链接的方式(当然除了 setting_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'));

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'));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生来就爱笑 2024-09-05 18:39:08

实际上你应该在 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。

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.

倒数 2024-09-05 18:39:08

您可以在 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',
        ));
    }
}

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',
        ));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文