我一直有一段时间让Sencha Touch 2.0有很多协会工作,特别是因为看起来他们的数据模型不直接允许多对多关系。我有两个模型 - 人物和角色(这些是更多,但这是本例中的两个重要),每个模型都有多对多。
我原本以为我可以在每个模型中使用hasMany执行此操作,但是数据在我的数据库中以第三范式存储,我认为我需要第三个人对角色模型。代码在这里:
Ext.define('SMToolkit.model.Person', {
extend: 'Ext.data.Model',
config: {
fields: [
'id',
'first_name',
'last_name',
'email',
'address',
'phone1',
'phone2',
'type',
'location'
],
hasMany: [
{
model: 'SMToolkit.model.Person_Role',
name: 'role'
}
],
proxy: {
type: 'rest',
url : 'index.php/api/persons'
}
}
});
Ext.define('SMToolkit.model.Role', {
extend: 'Ext.data.Model',
config: {
fields: [
'id',
'name',
'description',
'type',
'show_id'
],
hasMany: [
{
model: 'SMToolkit.model.Person_Role',
name: 'person'
},
{
model: 'SMToolkit.model.Scene_Role',
name: 'scene'
},
{
model: 'SMToolkit.model.Thing',
name: 'thing'
}
],
proxy: {
type: 'rest',
url : 'index.php/api/roles'
}
}
});
Ext.define('SMToolkit.model.Person_Role', {
extend: 'Ext.data.Model',
config: {
fields: [
'person_id',
'role_id'
],
associations: [
{
type: 'belongsTo',
model: 'SMToolkit.model.Person',
name: 'person'
},
{
type: 'belongsTo',
model: 'SMToolkit.model.Role',
name: 'role'
},
],
proxy: {
type: 'rest',
url : 'index.php/api/personsroles'
}
}
});
我已经确认上面的personroles url确实返回了一个有效的数据集,所以我知道那里应该有一些东西......
当我查看角色记录时,我可以看到相关商店的字段,但即使我确定数据库中的Person_Role表中有适当的记录,记录中的Persons数组也是空的。
我得到了这样的记录:
onRoleSelect: function(list, index, node, record) {
var editButton = this.getEditButton();
if (!this.showRole) {
this.showRole = Ext.create('SMToolkit.view.role.Show');
}
person = record.person();
thing = record.thing();
scene = record.scene();
person.load();
thing.load();
scene.load();
// Bind the record onto the view
this.showRole.setRecord(record);
// Push the show show view into the navigation view
this.getRoleContainer().push(this.showRole);
},
我做错了什么?为什么没有关联数据?
答案 0 :(得分:0)
这是处理Sencha中复杂模型关系的另一种方法。 我还没有测试过它,但我认为它也可能会处理很多关系。 递归的M-M关系可能会让你对linkChildAssociations()函数感到悲伤。