首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSData中的多到多关系支持

JSData中的多到多关系支持
EN

Stack Overflow用户
提问于 2015-10-28 17:40:43
回答 1查看 529关注 0票数 7

有没有办法在JSData中定义多到多的关系?

例如,我有以下三个表:

实体entityFile文件

在“实体”上,我希望有一个名为“file”的关系,它通过entityFile进行连接。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-29 04:04:07

问得好。一个典型的多对多关系就是一对多的关系:

任何实现中最重要的细节之一是:关系信息存储在哪里?这个问题的答案决定了如何访问一个实体的关系。让我们探讨几个选择。

前提:

A hasMany B

B hasMany A

选项1

关系信息存储在A的实例上。

在这个场景中,一旦您有了A实例,就可以找到它的关联B实例,因为关联的B实例的In存储在A上。这也意味着,如果您只有一个B实例,那么查找与B实例相关的A的所有实例的唯一方法是搜索A的所有实例,以查找其b_ids字段包含B实例的id的实例。

一个例子

代码语言:javascript
复制
var Player = store.defineResource({
  name: 'player',
  relations: {
    hasMany: {
      team: {
        // JSData will setup a "teams" property accessor on
        // instances of player which searches the store for
        // that player's teams
        localField: 'teams',
        localKeys: 'team_ids'
      }
    }
  }
})

var Team = store.defineResource({
  name: 'team',
  relations: {
    hasMany: {
      player: {
        localField: 'players',
        // Since relationship information is stored
        // on the player, in order to retrieve a
        // team's players we have to do a O(n^2)
        // search through all the player instances
        foreignKeys: 'team_ids'
      }
    }
  }
})

现在让我们看看它的作用:

代码语言:javascript
复制
var player = Player.inject({
  id: 1,
  team_ids: [3, 4]
})

// The player's teams aren't in the store yet
player.teams // [ ]

var player2 = Player.inject({
  id: 2,
  team_ids: [4, 5],
  teams: [
    {
      id: 4
    },
    {
      id: 5
    }
  ]
})

// See the property accessor in action
player2.teams // [{ id: 4 }, { id: 5 }]

// One of player one's teams is in the store now
player.teams // [{ id: 4 }]

// Access the relation from the reverse direction
var team4 = Team.get(4) // { id: 4 }

// The property accessor makes a O(n^2) search of the store because
// the relationship information isn't stored on the team
team4.players // [{ id: 1, team_ids: [3, 4] }, { id: 2, team_ids: [4, 5] }]

让我们从持久化层加载一个关系:

代码语言:javascript
复制
// To get an authoritative list of player one's 
// teams we ask our persistence layer.
// Using the HTTP adapter, this might make a request like this:
// GET /team?where={"id":{"in":[3,4]}} (this would be url encoded)
//
// This method call makes this call internally:
// Team.findAll({ where: { id: { 'in': player.team_ids } } })
player.DSLoadRelations(['team']).then(function (player) {

  // The adapter responded with an array of teams, which
  // got injected into the datastore.

  // The property accessor picks up the newly injected team3
  player.teams // [{ id: 3 }, { id: 4 }]

  var team3 = Team.get(3)

  // Retrieve all of team3's players.
  // Using the HTTP adapter, this might make a request like this:
  // // GET /player?where={"team_ids":{"contains":3}} (this would be url encoded)
  //
  // This method call makes this call internally:
  // Player.findAll({ where: { team_ids: { 'contains': team3.id } } })
  return team3.DSLoadRelations(['player'])
})

如果使用HTTP适配器,则由服务器解析查询字符串并使用正确的数据进行响应。如果您正在使用其他适配器之一,那么适配器已经知道如何返回正确的数据。在前端和后端使用JSData只会使这太容易。

选项2

关系信息存储在B的实例上。

这与选项1正好相反。

选项3

"A hasMany B“关系信息存储在实例A上,"B hasMany A”关系信息存储在B实例上。

这只是选项1,,只是现在它在两个方向上都能工作。

这种方法的一个优点是您可以从两个方向访问关系,而不需要使用foreignKeys选项。这种方法的一个缺点是,当关系发生变化时,必须在多个地方修改数据。

选项4

关系信息存储在枢轴(连接)表中。

A hasMany CC belongsTo A,其中实际的关系信息存储在C中。

B hasMany CC belongsTo B,其中实际的关系信息存储在C中。

举个例子:

代码语言:javascript
复制
var Player = store.defineResource({
  name: 'player',
  relations: {
    hasMany: {
      membership: {
        localField: 'memberships',
        // relationship information is stored on the membership
        foreignKey: 'player_id'
      }
    }
  }
})

var Team = store.defineResource({
  name: 'team',
  relations: {
    hasMany: {
      membership: {
        localField: 'memberships',
        // relationship information is stored on the membership
        foreignKey: 'team_id'
      }
    }
  }
})

和枢轴资源:

代码语言:javascript
复制
var Membership = store.defineResource({
  name: 'membership',
  relations: {
    belongsTo: {
      player: {
        localField: 'player',
        // relationship information is stored on the membership
        localKey: 'player_id'
      },
      team: {
        localField: 'team',
        // relationship information is stored on the membership
        localKey: 'team_id'
      }
    }
  }
})

现在让我们看看它的作用:

代码语言:javascript
复制
var player = Player.inject({ id: 1 })
var player2 = Player.inject({ id: 2 })
var team3 = Team.inject({ id: 3 })
var team4 = Team.inject({ id: 4 })
var team4 = Team.inject({ id: 5 })

player.memberships // [ ]
player2.memberships // [ ]
team3.memberships // [ ]
team4.memberships // [ ]
team5.memberships // [ ]

请注意,在这一点上我们还不能访问任何关系

代码语言:javascript
复制
// The relationships stored in our pivot table
var memberships = Membership.inject([
  {
    id: 997,
    player_id: 1,
    // player one is on team three
    team_id: 3
  },
  {
    id: 998,
    player_id: 1,
    // player one is also on team four
    team_id: 4
  },
  {
    id: 999,
    player_id: 2,
    // team four also has player 2
    team_id: 4
  },
  {
    id: 1000,
    player_id: 2,
    // player 2 is also on team 5
    team_id: 5
  }
])

现在我们有会员信息

代码语言:javascript
复制
player.memberships // [{ id: 997, ... }, { id: 998, ... }]
player2.memberships // [{ id: 998, ... }, { id: 999, ... }]
team3.memberships // [{ id: 997, ... }]
team4.memberships // [{ id: 998, ... }, { id: 999, ... }]
team5.memberships // [{ id: 1000, ... }]

现在,将您的枢轴表数据发送到您的前端并要求您的JavaScript对这些关系进行排序有点笨拙。为此,您需要一些助手方法:

代码语言:javascript
复制
var Player = store.defineResource({
  name: 'player',
  relations: {...},
  computed: {
    teams: {
      get: function () {
        return store.filter('membership', {
          player_id: this.id
        }).map(function (membership) {
          return store.get('team', membership.team_id)
        })
      }
    }
  },
  // Instance methods
  methods: {
    getTeams: function () {
      return Player.getTeams(this.id)
    }
  }
  // Static Class Methods
  getTeams: function (id) {
    return this.loadRelations(id, ['membership']).then(function (memberships) {
      return store.findAll('team', {
        where: {
          id: {
            'in': memberships.map(function (membership) {
              return membership.team_id
            })
          }
        }
      })
    })
  }
})

我将让您找出团队资源的类似方法。

如果您不想陷入帮助方法的麻烦,那么您可以在后端实现它们,使您的枢轴表对前端不可见,并使您的多到多关系看起来更像选项1、2或3。

有用链接

  • JSData文档
  • 关于多对多的文章
  • 简单的柱塞演示选项4
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33397943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档