PDA

View Full Version : belongsTo association returns a model of the wrong class



tm8747a
12 Jun 2013, 11:23 AM
I have the following models (edited for brevity):



Ext.define('NP.model.user.Userprofile', {
extend: 'Ext.data.Model',

idProperty: 'userprofile_id',
fields: [
{ name: 'userprofile_id', type: 'int' },
{ name: 'userprofile_username' }
]
});

Ext.define('NP.model.property.UnitTypeVal', {
extend: 'Ext.data.Model',

idProperty: 'unittype_val_id',
fields: [
{ name: 'unittype_val_id', type: 'int' },
{ name: 'unittype_id', type: 'int' },
{ name: 'unittype_val_val' }
]
});

Ext.define('NP.model.property.UnitType', {
extend: 'Ext.data.Model',

requires: [
'NP.model.user.Userprofile',
'NP.model.property.UnitTypeVal'
],

idProperty: 'unittype_id',
fields: [
{ name: 'unittype_id', type: 'int' },
{ name: 'unittype_name' },
{ name: 'unittype_updated_by', type: 'int' }
],

belongsTo: {
model : 'NP.model.user.Userprofile',
associationKey: 'lastUpdatedUser',
getterName : 'getLastUpdatedUser',
foreignKey : 'unittype_updated_by',
primaryKey : 'userprofile_id'
},
hasMany : {
model : 'NP.model.property.UnitTypeVal',
name : 'vals',
foreignKey: 'unittype_id',
primaryKey: 'unittype_id'
}
});


The hasMany relationship is working fine. The belongsTo, however, is pulling the wrong model. As in, if I do something like myRecord.getLastUpdatedUser() I do get a record back, except it's from some other model. The model it's giving me back was a model from another hasMany relationship for another model, so I tried removing it to see what happened. After doing that, I just get back some other random model that's not even used in any other relationship.

Any ideas???

tm8747a
13 Jun 2013, 6:36 AM
OK, figured this one out. I'll share just in case somebody runs into this too. Basically, turns out simplifying the code for brevity was a mistake, since I took out a key element. I was actually not using Ext.data.Model, but rather a class extending it that fixed a bug where using load() on the model doesn't take into account the idProperty configuration. So that extended class overrode the load function in statics, as posted on StackOverflow by somebody who obviously meant well. Problem is, looks like that screws something else up because as soon as I switched to Ext.data.Model, everything worked fine. Kind of a pain, since I can't upgrade to a new version that may fix the idProperty bug, and I don't really want to update the ExtJS source instead. But at least I've figured this issue out.