1. #1
    Sencha User
    Join Date
    Jul 2012
    Posts
    30
    Vote Rating
    3
    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
    St. Louis, MO
    Posts
    33,710
    Vote Rating
    436
    Answers
    3113
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      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
    ________________
    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.

  3. #3
    Sencha User
    Join Date
    Jul 2012
    Posts
    30
    Vote Rating
    3
    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
    27
    Vote Rating
    0
    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
    71
    Vote Rating
    0
    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?)