2012-03-19 102 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

回答