组合模型(模型中的模型)或模型之间的手动外键关联? [英] Composite models (models within models), or manual foreign key associations between models?
问题描述
Ext.regModel('OuterModel',{
fields:['name']
});
var outerStore = new Ext.data.Store({
model:'OuterModel',
data:[
{name:'item1'},
{name:'item2'}
]
});
Ext.regModel('InnerModel',{
fields:['a','b']
});
var innerStore = new Ext.data.Store({
model:'InnerModel',
data:[
{a:1,b:5},
{a:2,b:5},
{a:3,b:3}
]
});
每个 OuterModel
需要一个关联的 InnerModel
,我希望能够拥有一个可以创建的内部模型的内容。 s。我可以将 outerName
添加到 InnerModel
并在 outerName
上查询,但是这个手感很简单,手动管理这个关联。
手动解决方案是我唯一的选择,还是可以让模型成为另一个模型的一个领域?
模型可以通过belongsTo和hasMany关联与其他模型进行关联。例如,假设我们正在撰写一个涉及用户,帖子和评论的博客管理应用程序。我们可以表达这些模型之间的关系:
Ext.regModel('Post',{
fields: ['id','user_id'],
belongsTo:'User',
hasMany:{model:'Comment',name:'comments'}
});
Ext.regModel('Comment',{
fields:['id','user_id','post_id'],
belongsTo:'Post'
});
Ext.regModel('User',{
fields:['id'],
hasMany:[
'Post',
{model:'Comment',name:'comments'}
]
});
请参阅文档 Ext.data.BelongsToAssociation 和 Ext.data.HasManyAssociation 有关关联的用法和配置的详细信息。请注意,关联也可以这样指定:
Ext.regModel('User',{
fields:[ 'id'],
associations:[
{type:'hasMany',model:'Post',name:'posts'},
{type:'hasMany' ,model:'Comment',name:'comments'}
]
});
I have two models, and I want to "nest" one within the other. Is there a way to do this with Sencha Touch? My models look like this:
Ext.regModel('OuterModel', {
fields: ['name']
});
var outerStore = new Ext.data.Store({
model: 'OuterModel',
data: [
{name: 'item1'},
{name: 'item2'}
]
});
Ext.regModel('InnerModel', {
fields: ['a', 'b']
});
var innerStore = new Ext.data.Store({
model: 'InnerModel',
data: [
{a: 1, b: 5},
{a: 2, b: 5},
{a: 3, b: 3}
]
});
Each OuterModel
needs an associated InnerModel
, and I would love to just be able to have a field that was an inner-model that I could create Ext.List
s from. I could add outerName
to InnerModel
and query on outerName
, but this feels sloppy, manually managing the association.
Is the manual solution my only option, or can I make a model a field of another model?
Models can have associations with other Models via belongsTo and hasMany associations. For example, let's say we're writing a blog administration application which deals with Users, Posts and Comments. We can express the relationships between these models like this:
Ext.regModel('Post', {
fields: ['id', 'user_id'],
belongsTo: 'User',
hasMany : {model: 'Comment', name: 'comments'}
});
Ext.regModel('Comment', {
fields: ['id', 'user_id', 'post_id'],
belongsTo: 'Post'
});
Ext.regModel('User', {
fields: ['id'],
hasMany: [
'Post',
{model: 'Comment', name: 'comments'}
]
});
See the docs for Ext.data.BelongsToAssociation and Ext.data.HasManyAssociation for details on the usage and configuration of associations. Note that associations can also be specified like this:
Ext.regModel('User', {
fields: ['id'],
associations: [
{type: 'hasMany', model: 'Post', name: 'posts'},
{type: 'hasMany', model: 'Comment', name: 'comments'}
]
});
这篇关于组合模型(模型中的模型)或模型之间的手动外键关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!