1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    14
    Vote Rating
    1
    jonmurua is on a distinguished road

      0  

    Default Answered: Bug in hasMany association in a model?

    Answered: Bug in hasMany association in a model?


    Hello:

    I am trying to read the following JSON:

    Code:
    [ {
        communicationcontactid : 5,
        firstname : "J",
        lastname : "M",
        addresses : [ {
            addressid : 50,
            addresstype : "PRIMARY",
            addressline1 : "Road",
            addressline2 : null,
            addressline3 : null,
            addressline4 : null,
            addressstatus : null,
            country : "UK",
            postcode : null,
            statecounty : null
        } ],
        contactdetails : [ {
            contactdetailsid : 50,
            contactdetailstype : "PRIMARY",
            email : "j@gmail.com",
            fax : null,
            homephone : "346",
            workphone : "346"
        } ]
    } ]
    For that, I have the following model:

    Code:
    Ext.define('PlatformOneFE.model.Contact', {
        extend : 'Ext.data.Model',
    
    
        requires : [ 'PlatformOneFE.model.Address',
                'PlatformOneFE.model.ContactDetails' ],
    
    
        config : {
            fields : [ {
                name : 'communicationcontactid',
                type : 'int'
            }, {
                name : 'firstname',
                type : 'string'
            }, {
                name : 'lastname',
                type : 'string'
            }, ],
    
    
            hasMany : [ {
                model : 'PlatformOneFE.model.Address',
                name : 'addresses',
            }, {
                model : 'PlatformOneFE.model.ContactDetails',
                name : 'contactdetails',
            }, ],
        }
    });
    When I process the data, the addresses store is correctly created using the Address model, however, but the contactdetails is not created.

    If I define the contactdetails field as a field in the Contact model, it is loaded as an array (of course not a store).

    Code:
               {
                name : 'contactdetails',
                type : 'auto'
                    }
    Firebugs' console.log(record) is:

    Code:
    Ext.apply.create.Class    
         _data: Object
         addresses: Array[1]
         0: Object
         length: 1
         __proto__: Array[0]
         communicationcontactid: 5
         contactdetails: Array[0]
         firstname: "J"
         id: "ext-record-3"
         lastname: "M"
         __proto__: Object
    addressesStore: Ext.apply.create.Class
    data: Object 
    ...
    The associated models are below:

    Code:
    Ext.define('PlatformOneFE.model.ContactDetails', {
        extend : 'Ext.data.Model',
    
    
        config : {
            fields : [ {
                name : 'contactdetailsid',
                type : 'int'
            }, {
                name : 'contactdetailstype',
                type : 'string'
            }, {
                name : 'email',
                type : 'string'
            }, {
                name : 'fax',
                type : 'string'
            }, {
                name : 'homephone',
                type : 'string'
            }, {
                name : 'workphone',
                type : 'string'
            }, ],
            
            belongsTo: 'PlatformOneFE.model.Contact'
            
        }
    
    
    });
    Code:
    Ext.define('PlatformOneFE.model.Address', {
        extend : 'Ext.data.Model',
    
    
        config : {
            fields : [ {
                name : 'addressid',
                type : 'int'
            }, {
                name : 'addresstype',
                type : 'string'
            }, {
                name : 'addressline1',
                type : 'string'
            }, {
                name : 'addressline2',
                type : 'string'
            }, {
                name : 'addressline3',
                type : 'string'
            }, {
                name : 'addressline4',
                type : 'string'
            }, {
                name : 'addressstatus',
                type : 'string'
            }, {
                name : 'country',
                type : 'string'
            }, {
                name : 'postcode',
                type : 'string'
            }, {
                name : 'statecounty',
                type : 'string'
            }, ],
            
            belongsTo: 'PlatformOneFE.model.Contact'
            
        }
    
    
    });

    Any idea why that is happening?

  2. In the hasMany configs, add associationKey to be set to the same values as the name. Does that work?

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,780
    Answers
    3465
    Vote Rating
    833
    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


    In the hasMany configs, add associationKey to be set to the same values as the name. Does that work?
    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.

  4. #3
    Sencha User
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    498
    Answers
    25
    Vote Rating
    51
    shepsii has a spectacular aura about shepsii has a spectacular aura about shepsii has a spectacular aura about

      0  

    Default


    I have the same issue on a near identical example - unfortunately the associationKey doesn't work... I've always felt uneasy using associations in either version of sencha touch, I never know whether they're going to work or not!

  5. #4
    Sencha User
    Join Date
    Feb 2012
    Posts
    14
    Vote Rating
    1
    jonmurua is on a distinguished road

      0  

    Thumbs up Thanks a lot!

    Thanks a lot!


    Thanks! That helped!

    This is the model that worked:

    Code:
    Ext.define('PlatformOneFE.model.Contact', {
        extend : 'Ext.data.Model',
    
    
        requires : [ 'PlatformOneFE.model.Address',
                'PlatformOneFE.model.ContactDetails' ],
    
    
        config : {
            fields : [ {
                name : 'communicationcontactid',
                type : 'int'
            }, {
                name : 'firstname',
                type : 'string'
            }, {
                name : 'lastname',
                type : 'string'
            }, ],
    
    
            hasMany : [ {
                model : 'PlatformOneFE.model.Address',
                name : 'addresses',
                associationKey: 'addresses'
            }, {
                model : 'PlatformOneFE.model.ContactDetails',
                name : 'contactdetails',
                associationKey: 'contactdetails'
            }, ],
        }
    
    
    });
    Now, I am unsure whether I should use associated models or a TreeStore, as the JSON data brings all the details in a tree, and not references and ids for inner models/resources...

  6. #5
    Sencha User
    Join Date
    Oct 2011
    Location
    Germany
    Posts
    146
    Answers
    4
    Vote Rating
    10
    Möhre will become famous soon enough

      0  

    Default


    Quote Originally Posted by shepsii View Post
    I've always felt uneasy using associations in either version of sencha touch, I never know whether they're going to work or not!
    me too.

  7. #6
    Sencha User
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    498
    Answers
    25
    Vote Rating
    51
    shepsii has a spectacular aura about shepsii has a spectacular aura about shepsii has a spectacular aura about

      0  

    Default


    So now I have data reading in (I set a proxy on the owned model - bad idea), I need to save all the data localstorage - that is, a store with records of a model that with a hasMany association to another. Despite hours of trying I don't think this is possible - can someone at Sencha confirm?

  8. #7
    Sencha User
    Join Date
    Feb 2012
    Posts
    14
    Vote Rating
    1
    jonmurua is on a distinguished road

      0  

    Default


    I am working on something similar, using a rest proxy (in the holder model).
    I haven't completed it yet and I am not sure how that will work;I expect the content/store of the associated (inner) model to be contained in the holder model, thus, on .save it should send all the data to the rest service. But as it is a separate store, I am unsure if that will work.

    In addition, I suspect that there should be an easy way to save the content of the associated model independently. Say:

    POST /Contact
    Creates a new contact (with addresses if provided in the store)

    POST /Contact/5/Address
    Creates a new address in Contact with id 5

    The link should be automatic, based in the belongsTo association of the inner model.
    @shepsii, did you add the belongsTo association?

  9. #8
    Sencha User
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    498
    Answers
    25
    Vote Rating
    51
    shepsii has a spectacular aura about shepsii has a spectacular aura about shepsii has a spectacular aura about

      0  

    Default


    I did add the belongsTo, yes... The issue I am having now is with saving back to localstorage the owned store, it isn't saved along with the record of its owner. Localstorage is made so tough in ST2 mainly because of the assumption that a record with an id doesn't need to be saved... what if I want it to exist in both localstorage and on my server?

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi