Results 1 to 3 of 3

Thread: problems with models and hasMany associations

  1. #1
    Sencha User
    Join Date
    Nov 2007
    Posts
    25
    Vote Rating
    0
      0  

    Default Answered: problems with models and hasMany associations

    Hello,

    I'm having problems with models and hasMany associations. Here is my schema (all models and store are registered in the app):

    Code:
    Ext.define('FMSP.model.Product', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
              'id',
              'sku',
              'name'
            ],
            hasMany  : [
                {model: 'FMSP.model.ProductColor', name: 'colors'}
            ]
        }
    });
    
    
    Ext.define('FMSP.model.ProductColor', {
        extend: 'Ext.data.Model',
        config: {
            fields: ['name','nextProductId'],
            belongsTo: 'FMSP.model.Product'
        }
    });
    
    
    Ext.define('FMSP.store.Product', {
       extend: 'Ext.data.Store',
       alias: 'store.product',
       require:['FMSP.model.Product'],
       config:     {
    	   model: 'FMSP.model.Product',
           proxy: {
                type: 'ajax',
                url : 'https://localhost:8443/service.ser?type=category&cat=27665&textSearch=&isOnSale=false',
                reader: {
                    type: 'json',
                    rootProperty: 'products'
                }
           }
       }
    });
    The AJAX service returns this JSON:

    Code:
    {
      "products": [
        {
          "id": "6148914691233432687",
          "sku": "499197",
          "name": "Wedge sandal",
          "colors": [
            {
              "name": "Tan",
              "nextProductId": "6148914691233432687"
            }
          ]
        }  
      ]
    }
    In the Chrome console I can successfully do:

    var s = Ext.create('FMSP.store.Product')
    s.load()
    var p = s.getAt(0)
    p.get('sku') (returns "499197")

    And if I do:

    p.getAssociatedData()

    it returns an object that shows the colors association BUT always with an empty array although p.raw contains an object that as a colors array with one item. What I'm doing wrong?


    Thanks,
    Andrea

  2. You need to add associationKey to your hasMany config:

    Code:
    Ext.define('FMSP.model.Product', {
        extend: 'Ext.data.Model',
    
        config: {
            fields: [
                'id',
                'sku',
                'name'
            ],
            hasMany  : [
                { model : 'FMSP.model.ProductColor', name : 'colors', associationKey : 'colors' }
            ]
        }
    });
    associationKey tells the reader what property to read the data from which defaults to the associationName + '_id' which in your case it would look for the 'ProductColor_id' property which doesn't exist. Adding associationKey : 'colors' tells it to look at the colors property which does exist!

  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

    You need to add associationKey to your hasMany config:

    Code:
    Ext.define('FMSP.model.Product', {
        extend: 'Ext.data.Model',
    
        config: {
            fields: [
                'id',
                'sku',
                'name'
            ],
            hasMany  : [
                { model : 'FMSP.model.ProductColor', name : 'colors', associationKey : 'colors' }
            ]
        }
    });
    associationKey tells the reader what property to read the data from which defaults to the associationName + '_id' which in your case it would look for the 'ProductColor_id' property which doesn't exist. Adding associationKey : 'colors' tells it to look at the colors property which does exist!
    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 User
    Join Date
    Nov 2007
    Posts
    25
    Vote Rating
    0
      0  

    Default

    Thanks a lot! Now it works perfectly.

Posting Permissions

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