2012-03-19 101 views
2

我一直有一段时间让Sencha Touch 2.0 hasMany协会工作,特别是因为它看起来像他们的数据模型不直接允许多对多的关系。我有两个模型 - 人物和角色(这里有更多,但这两个在这个例子中很重要),每个人都有一对多。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' 
    } 
} 

}); 

我已经证实了personsroles以上网址事实上确实返回一个有效的数据集,所以我知道应该有有什么...

当我看一个角色记录,我可以看到关联商店的字段,但即使我确定在数据库的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

此事进一步复杂化,甚至如果我能得到associatin以上,它只是给我Person_Role记录的人,我不知道如何遍历从有到实际人员记录 – 2012-03-19 23:41:17

回答