- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法在 JSData 中定义多对多关系? ?
例如,我有这 3 个表:
实体
实体文件
文件
在“实体”上,我想要一个名为"file"的关系,它通过实体文件进行连接。
最佳答案
好问题。典型的多对多关系就是两个一对多关系:
任何实现中更重要的细节之一是:
关系信息存储?这个问题的答案决定了
可以访问实体的关系。让我们探索几个选项。
前提:A
hasMany B
B
hasMany A
选项 1
关系信息存储在 A
的实例上.
在这种情况下,一旦您拥有 A
的实例你可以找到它的关联B
的实例,因为关联的 ID B
实例存储在A
.这也意味着如果您只有 B
的实例,唯一的办法
找到 A
的所有实例B
实例涉及到
搜索 A
的所有实例对于那些 b_ids
字段包含id
的B
实例。
一个例子
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'
}
}
}
})
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] }]
// 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'])
})
B
的实例上.
A
hasMany
B
”关系信息存储在实例
A
上, 和
B
hasMany
A
”关系信息存储在
B
的实例上.
foreignKeys
选项。一个缺点
A
hasMany
C
和
C
属于
A
, 实际关系
C
.
B
hasMany
C
和
C
属于
B
, 实际关系
C
.
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'
}
}
}
})
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'
}
}
}
})
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 // [ ]
// 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
}
])
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, ... }]
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
})
}
}
})
})
}
})
关于jsdata - JSData 中的多对多关系支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33397943/
有没有办法在 JSData 中定义多对多关系? ? 例如,我有这 3 个表: 实体 实体文件 文件 在“实体”上,我想要一个名为"file"的关系,它通过实体文件进行连接。 最佳答案 好问题。典型的多
如果我调用 dataStore.findAll(mapperName, params, opts) 其中 params 是 { where: { cc: { '===': 'tes
JSData 是替代还是补充 Sequelize.js? 我试图了解这两个库如何一起玩以及是否应该一起玩。 从我的阅读来看,JSData 主要处理访问数据,而 Sequelize.js 将创建和修改
我需要设置我的 JSData 配置以传递基于 cookie 的 session 身份验证的信息以及 CSRF header 。 最佳答案 实例化 HttpAdapter 时,使用以下设置 withCr
我是一名优秀的程序员,十分优秀!