1. #1
    Sencha User
    Join Date
    Nov 2007
    Posts
    25
    Vote Rating
    0
    andreik65 is on a distinguished road

      0  

    Default Answered: problems with models and hasMany associations

    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 - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,754
    Answers
    3462
    Vote Rating
    827
    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


    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 @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
    Nov 2007
    Posts
    25
    Vote Rating
    0
    andreik65 is on a distinguished road

      0  

    Default


    Thanks a lot! Now it works perfectly.

Thread Participants: 1

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