-
9 Sep 2012 3:05 AM #1
Unanswered: hasOne association
Unanswered: hasOne association
Hi there,
I ran into an issue, where I needed to reference the same model twice using 'hasOne' association. In this case its an address model, which is referenced twice. I got the following setup:
But it did not work as expected (not the first hasOne), the latter instance was ignored, no errors, no xhr calls. However after checking the source, I found out that the reference to the association is stored by the model name. That was the reason, why I had only _one_ reference in the ownerModel, since it was overwritten.Code:associations : [{ type : 'hasOne', model : 'Address', primaryKey : 'id', foreignKey : 'address_id', getterName : 'getAddress' }, { type : 'hasOne', model : 'Address', associationKey : 'billingAddress', // not sure what it does, is currently ignored by ExtJS?! primaryKey : 'id', foreignKey : 'billing_address_id', getterName : 'getBillingAddress' }]
adding
to the hasOne constructor, gives me the expected behavior.Code:associationKey = Ext.String.capitalize(associationKey); me.instanceName = associatedName + associationKey +'HasOneInstance';
Now, is this a bug/missing feature or did I miss some property, which would generate the instanceName proeprly?
-
17 Sep 2012 8:35 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,684
- Vote Rating
- 435
- Answers
- 3111
associationKey is the property in the data that should be used for the associated data. You can either specify it in your config or use the associatedName and it will create the associationKey based on it. If you haven't specified the associationName it will use the associatedModel config.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
17 Sep 2012 11:20 AM #3
As far as I understood the code, it was/is ignored. We tried several ways to achieve this. We specified different assiciatioName, different associationKey but none of them worked. In fact the property name to reference the association was always the same and therefore was overriden by the 2nd association. The only way I got it working is adjusting the property name as shown above.
Probably the case was not covered by the associations, but sometimes you need the same model twice such as user_id and last_edit_user_id or in the case above an address and billing address.
-
18 Dec 2012 8:51 AM #4
I've ready many posts about this problem. I've never seen an answer that actually worked. Here is what is going on (at least in Ext 4.1)
in Ext.data.Model
which means that Ext.data.Model.itemNameFn will be used in Ext.util.AbstractMixedCollection.add if no key argument is used. Model.itemNameFn isCode:associationsMixedCollection = new Ext.util.MixedCollection(false, prototype.itemNameFn),
so in Ext.data.Model, the lineCode:itemNameFn: function(item) { return item.name; }
causes the created association's name property to be used as the key. If you do not explicitly set the name property in the association config, then it will be the model's name. So multiple associations to the same model over write each other in the associationsMixedCollectionCode:associationsMixedCollection.add(Ext.data.association.Association.create(associationConfig));
Here is a model config that actually works with multiple associations to the same model
Code:hasOne: [ { instanceName: 'billTo', name: 'billTo', associationKey: 'billTo', model: 'CrmApp.model.Contact', getterName: 'getBillTo', foreignKey: 'billToId', setterName: 'setBillTo' }, { instanceName: 'shipTo', name: 'shipTo', associationKey: 'shipTo', model: 'CrmApp.model.Contact', getterName: 'getShipTo', foreignKey: 'shipToId', setterName: 'setShipTo' } ]
-
18 Apr 2013 4:48 AM #5
I dont see the name property in the hasOne fields?
But i see the name property in the hasMany
In extjs 4.1.3 docs, has it changed or its missing.
I come across lot of time that some property are described in blog but not there in docs (why this inconsistency?)


Reply With Quote