PDA

View Full Version : Many assocations to one model



K4T
26 May 2012, 8:08 AM
Hello! I have task model which have two associations to one model - User model. Those associations create two getters for me: getAssignedTo() and getAssignedBy() which should allow me to get users names by id (foreign keys). I think those associations are collide and I dont know how to resolve this. For example:



console.log(record);

record.getAssignedTo(function(user, operation) {
console.log('Assigned To: ' + user.get('name'));
}, this );


record.getAssignedBy(function(user, operation) {
console.log('Assigned By: ' + user.get('name'));
}, this );


That example will log:

35646

As you see getAssignedBy() returns empty string :/ If I remove accocation for assignedToId foreign key than getter getAssignedBy() will work because there is only one assocation to User model inside Task model. How I can resolve this issue?

Task Model:

Ext.define('Tasks.model.Task', {
extend: 'Ext.data.Model',


fields: [
{name: 'id', type: 'int'},
...
{name: 'assignedToId', type: 'int', defaultValue: 1}, //default value for simulating logged user
{name: 'assignedById', type: 'int', defaultValue: 1} //default value for simulating logged user
],


associations: [
{ type: 'belongsTo', model: 'Tasks.model.User', primaryKey: 'id', foreignKey: 'assignedToId', getterName: 'getAssignedTo' },
{ type: 'belongsTo', model: 'Tasks.model.User', primaryKey: 'id', foreignKey: 'assignedById', getterName: 'getAssignedBy' }
],


proxy: {
...
}


});


User Model:


Ext.define('Tasks.model.User', {
extend: 'Ext.data.Model',


fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'}
],


hasMany: {model: 'Tasks.model.Task', name: 'tasks'},


proxy: {
...
}


});

el_chief
26 May 2012, 1:19 PM
Rules for BelongsTo Associations in ExtJS (http://extjs-tutorials.blogspot.ca/2012/05/extjs-belongsto-association-rules.html)

Rules for HasMany Relationships in ExtJS
(http://extjs-tutorials.blogspot.ca/2012/05/extjs-hasmany-relationships-rules.html)