Results 1 to 8 of 8

Thread: Bug in hasMany association in a model?

  1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    14
    Vote Rating
    1
      0  

    Default 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 - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3932
    Vote Rating
    1272
      0  

    Default

    In the hasMany configs, add associationKey to be set to the same values as the name. Does that work?
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  4. #3
    Sencha Premium Member
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    587
    Answers
    25
    Vote Rating
    88
      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
      0  

    Thumbs up 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
      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 Premium Member
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    587
    Answers
    25
    Vote Rating
    88
      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
      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 Premium Member
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    587
    Answers
    25
    Vote Rating
    88
      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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •