1. #1
    Sencha User
    Join Date
    Jul 2012
    Posts
    33
    Vote Rating
    4
    Answers
    4
    Alexander Bauer is on a distinguished road

      0  

    Default 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:

    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'
        }]
    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.
    adding
    Code:
            associationKey = Ext.String.capitalize(associationKey);
            me.instanceName = associatedName + associationKey +'HasOneInstance';
    to the hasOne constructor, gives me the expected behavior.

    Now, is this a bug/missing feature or did I miss some property, which would generate the instanceName proeprly?

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,541
    Vote Rating
    872
    Answers
    3566
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    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
    ________________
    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 in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Jul 2012
    Posts
    33
    Vote Rating
    4
    Answers
    4
    Alexander Bauer is on a distinguished road

      0  

    Default


    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.

  4. #4
    Sencha User
    Join Date
    Sep 2012
    Posts
    31
    Vote Rating
    1
    Answers
    2
    billsalvucci is on a distinguished road

      0  

    Default


    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

    Code:
    associationsMixedCollection = new Ext.util.MixedCollection(false, prototype.itemNameFn),
    which means that Ext.data.Model.itemNameFn will be used in Ext.util.AbstractMixedCollection.add if no key argument is used. Model.itemNameFn is
    Code:
    itemNameFn: function(item) {
            return item.name;
        }
    so in Ext.data.Model, the line

    Code:
    associationsMixedCollection.add(Ext.data.association.Association.create(associationConfig));
    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 associationsMixedCollection

    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'
            }
        ]

  5. #5
    Sencha Premium Member
    Join Date
    Nov 2012
    Location
    Bangalore
    Posts
    79
    Vote Rating
    1
    rupamkhaitan is on a distinguished road

      0  

    Default


    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?)

  6. #6
    Sencha User
    Join Date
    May 2013
    Posts
    3
    Vote Rating
    1
    yuval.gilboa is on a distinguished road

      0  

    Default


    I stumbled across same problem. This is a bug in Sencha architect, which for some reason does not expose the name/associatedName config properties on HasOne assocations. To overcome this bug, use the following override:

    Code:
    Ext.override(Ext.data.association.HasOne, {
       constructor: function(config) {
          if (config.associationKey) {
             Ext.applyIf(config, {
                associatedName: config.associationKey,
                getterName: 'get' + Ext.String.capitalize(config.associationKey),
                setterName: 'set' + Ext.String.capitalize(config.associationKey)
             })
          }
          this.callParent([config])
       }
    });
    I'm using here the associationKey to set the associatedName, since associationKey is exposed for configuration, and typically the associatedName will be the same as the associationKey.

    Hope this helps,
    Yuval

  7. #7
    Sencha User
    Join Date
    May 2013
    Posts
    3
    Vote Rating
    1
    yuval.gilboa is on a distinguished road

      1  

    Default


    I also found another, simpler, solution. You can set the name property on the HasOne association by adding it as a custom property. See http://www.sencha.com/forum/showthread.php?259418-Add-custom-property-to-model-with-Sencha-Architect-2

    Yuval