Success! Looks like we've fixed this one. According to our records the fix was applied for TOUCH-500 in a recent build.
  1. #1
    Ext JS Premium Member
    Join Date
    Apr 2008
    Location
    San Francisco, CA
    Posts
    10
    Vote Rating
    0
    stormin_walker is on a distinguished road

      0  

    Exclamation Can't have more than 1 hasMany association

    Can't have more than 1 hasMany association


    I'm guessing I can have more than 1 hasMany association, but it's not working. I can connect one of the associations (always just the first one defined in the "hasMany" array), but never both.

    It's easy to recreate. If I add the following to the nestedLoading.js example (in KitchenSink) it breaks:

    Code:
    Ext.regModel('User', {
        fields: ['id', 'name'],
        
        hasMany: [{model: 'Order', name: 'orders'},{model: 'Phone', name: 'phones'}],
        
        proxy: {
            type: 'ajax',
            url : 'src/demos/data/userData.json'
        }
    });
    
    Ext.regModel('Phone', {
        fields: ['id', 'number']
    });
    
    Ext.regModel('Order', {
        fields: ['id', 'status'],
        
        hasMany: {model: 'OrderItem', name: 'orderItems'}
    });
    
    Ext.regModel('OrderItem', {
        fields: ['id', 'quantity', 'price', 'name']
    });
    Now just add some phones to the userData.json:

    Code:
    ...
        {
            "id": 1,
            "name": "Ed Spencer",
    	"phones": [{
    		"id": 1,
    		"number": "123456789"
    	}],
            "orders": [
    ...
    Note: when doing this it's just the record for "Ed Spencer" that's broken. So it appears that it's having trouble building the child stores from the JSON.

    (BTW: the Sencha forum has become Japanese - my settings are US English but for some reason everything is Japanese all of a sudden - thankfully Chrome translates the pages for me

  2. #2
    Ext User
    Join Date
    Jul 2010
    Posts
    3
    Vote Rating
    0
    hq242 is on a distinguished road

      0  

    Default


    Hi there,

    I've noticed the same issue. Defining 2 "hasMany" associations in the same model will just get one child store but never both child stores for both "hasMany" associations.

  3. #3
    Ext JS Premium Member
    Join Date
    Jul 2008
    Location
    Miami, FL
    Posts
    56
    Vote Rating
    1
    omarc is on a distinguished road

      0  

    Default


    This might have something to do with it, try changing the readAssociated function in Ext.data.Reader as indicated here:

    Code:
        readAssociated: function(record, data) {
            var associations = record.associations.items,
                length       = associations.length,
                association, associationName, associationData, proxy, reader, store, i;
    
            for (i = 0; i < length; i++) {
                association     = associations[i];
                associationName = association.name;
                associationData = data[association.associationKey || associationName];
    
                if (associationData) {
    //ADD THESE 2 LINES HERE
                    reader = null;
                    proxy = null;
    
                    proxy = association.associatedModel.getProxy();
    
                    // if the associated model has a Reader already, use that
                    if (proxy) {
                        reader = proxy.getReader();
                    }
    See if after doing this change you still have the same problem with more than one hasMany.

    I found this to be a quick fix for having two associations, one a hasMany and the other a belongsTo.

    Let me know if that works for you.

    Omar

  4. #4
    Sencha - Community Support Team edspencer's Avatar
    Join Date
    Jan 2009
    Location
    Palo Alto, California
    Posts
    1,939
    Vote Rating
    7
    edspencer is a jewel in the rough edspencer is a jewel in the rough edspencer is a jewel in the rough

      0  

    Default


    I've fixed this in Reader.js now and added unit tests to confirm. This will work correctly in the next release. Thanks for you patience while we sorted this out.
    Ext JS Senior Software Architect
    Personal Blog: http://edspencer.net
    Twitter: http://twitter.com/edspencer
    Github: http://github.com/edspencer

  5. #5
    Ext User
    Join Date
    Oct 2010
    Posts
    4
    Vote Rating
    0
    ShiroiSame is on a distinguished road

      0  

    Default


    Ok but... when the next release?!

    Because I need to fix this now...

    @omarc: your solution works... great!!!

  6. #6
    Sencha - Community Support Team edspencer's Avatar
    Join Date
    Jan 2009
    Location
    Palo Alto, California
    Posts
    1,939
    Vote Rating
    7
    edspencer is a jewel in the rough edspencer is a jewel in the rough edspencer is a jewel in the rough

      0  

    Default


    Should be Monday 28th. This override should get you by until then - just include it after Sencha Touch but before your application:

    Code:
    Ext.override(Ext.data.Reader, {
        readAssociated: function(record, data) {
            var associations = record.associations.items,
                length       = associations.length,
                association, associationName, associatedModel, associationData, inverseAssociation, proxy, reader, store, i;
            
            for (i = 0; i < length; i++) {
                association     = associations[i];
                associationName = association.name;
                associationData = this.getAssociatedDataRoot(data, association.associationKey || associationName);
                associatedModel = association.associatedModel;
                
                if (associationData) {
                    proxy = associatedModel.proxy;
                    
                    // if the associated model has a Reader already, use that, otherwise attempt to create a sensible one
                    if (proxy) {
                        reader = proxy.getReader();
                    } else {
                        reader = new this.constructor({
                            model: association.associatedName
                        });
                    }
                    
                    if (association.type == 'hasMany') {
                        store = record[associationName]();
                        
                        store.add.apply(store, reader.read(associationData).records);
                        
                        //now that we've added the related records to the hasMany association, set the inverse belongsTo
                        //association on each of them if it exists
                        inverseAssociation = associatedModel.prototype.associations.findBy(function(assoc) {
                            return assoc.type == 'belongsTo' && assoc.associatedName == record.constructor.modelName;
                        });
                        
                        //if the inverse association was found, set it now on each record we've just created
                        if (inverseAssociation) {
                            store.data.each(function(associatedRecord) {
                                associatedRecord[inverseAssociation.instanceName] = record;
                            });                        
                        }
    
                    } else if (association.type == 'belongsTo') {
                        record[association.instanceName] = reader.read([associationData]).records[0];
                    }
                }
            }
        }
    });
    Ext JS Senior Software Architect
    Personal Blog: http://edspencer.net
    Twitter: http://twitter.com/edspencer
    Github: http://github.com/edspencer

  7. #7
    Sencha User
    Join Date
    Dec 2010
    Posts
    18
    Vote Rating
    1
    Jeff Blake is on a distinguished road

      0  

    Default


    What's the story with this bug? The above solution did not work.

    The weird thing with my case is that if I change the order of the includes for models in the index.html file - it either produces a "property x is undefined"
    If I switch the order of the hasMany models, I get no error but instead no data is able to display from the second model.

    This is a weird issue, any ideas?

  8. #8
    Sencha User
    Join Date
    Dec 2010
    Posts
    18
    Vote Rating
    1
    Jeff Blake is on a distinguished road

      0  

    Default


    Here is some more information regarding the bug with hasMany:

    Models:
    Code:
    Ext.regModel("Store", {
        fields: [
            {name: "id",                type: "int"},
    		{name: "name",              type: "string"},
    		{name: "email",             type: "string"},
            {name: "phone",             type: "string"},
            {name: "hashed_password",   type: "string"}
        ],
    
    	validations: [
    	        {type: 'presence',  field: 'hashed_password'},
    			{type: 'presence',  field: 'name'},
    	        {type: 'length',    field: 'hashed_password',     min: 4},
    	        {type: 'format',    field: 'email', matcher: /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i}
    	    ],
    	    associations: [
    	        {type: 'hasMany', model: 'Deal', name: 'deals'},
    	 {type: 'hasMany', model: 'Earn', name: 'earns'},
    	 {type: 'hasMany', model: 'Reward', name: 'rewards'}
    	    ],
    
    
    
    });
    /**
     * @class Deal
     * @extends Ext.data.Model
     */
    Ext.regModel("Deal", {
        fields: [
            {name: "id",                type: "int"},
    		{name: "name",       		type: "string"},
    		{name: "store_id",          type: "integer"},
            {name: "description",       type: "string"},
            {name: "thumbs_up",         type: "integer"},
    		{name: "thumbs_down",       type: "integer"}
        ],
    
    	validations: [
    	       // {type: 'presence',  field: 'hashed_password'}
    	    ],
        belongsTo: 'Store'
    		
    });
    I ommited the other two for brevity

    Requested JSON Data:
    Code:
    {
        "stores": [
            {
                "name": "Jeffs store",
                "created_at": "2010-12-28T10:04:08Z",
                "updated_at": "2011-01-19T00:39:44Z",
                "hashed_password": "67ea66ac0f272be8aac494de523892bf0bb646b7",
                "id": 1,
                "network": "ubc",
                "area": null,
                "rewards": [
                    {
                        "name": "Reward 1",
                        "created_at": "2011-01-21T08:56:39Z",
                        "updated_at": "2011-01-21T08:56:39Z",
                        "store_id": 1,
                        "id": 1,
                        "points_required": 1
                    },
                    {
                        "name": "Reward 2",
                        "created_at": "2011-01-24T22:02:45Z",
                        "updated_at": "2011-01-24T22:02:45Z",
                        "store_id": 1,
                        "id": 2,
                        "points_required": 2
                    }
                ],
                "phone": "23123",
                "registered_at": "2010-12-28T10:03:00Z",
                "deals": [
                    {
                        "name": null,
                        "created_at": "2010-12-29T04:25:13Z",
                        "updated_at": "2011-01-21T08:28:04Z",
                        "store_id": 1,
                        "id": 1,
                        "active_date": null,
                        "thumbs_down": null,
                        "description": null,
                        "thumbs_up": null,
                        "created": null
                    },
                    {
                        "name": "My deal",
                        "created_at": "2010-12-29T04:28:06Z",
                        "updated_at": "2010-12-29T04:28:06Z",
                        "store_id": 1,
                        "id": 2,
                        "active_date": "2010-12-29T04:27:00Z",
                        "thumbs_down": null,
                        "description": "dealie",
                        "thumbs_up": null,
                        "created": null
                    },
                    {
                        "name": "HEfd",
                        "created_at": "2010-12-29T04:33:29Z",
                        "updated_at": "2010-12-29T04:33:29Z",
                        "store_id": 1,
                        "id": 3,
                        "active_date": "2010-12-29T04:33:00Z",
                        "thumbs_down": null,
                        "description": "sdsdd",
                        "thumbs_up": null,
                        "created": null
                    },
                    {
                        "name": "Ssdsd",
                        "created_at": "2011-01-09T07:34:55Z",
                        "updated_at": "2011-01-09T07:34:55Z",
                        "store_id": 1,
                        "id": 4,
                        "active_date": "2011-01-09T07:34:00Z",
                        "thumbs_down": null,
                        "description": "dasdasdasdasd",
                        "thumbs_up": null,
                        "created": null
                    },
                    {
                        "name": null,
                        "created_at": "2011-01-14T00:57:30Z",
                        "updated_at": "2011-01-14T00:57:30Z",
                        "store_id": 1,
                        "id": 6,
                        "active_date": null,
                        "thumbs_down": null,
                        "description": null,
                        "thumbs_up": null,
                        "created": null
                    }
                ],
                "earns": [
                    {
                        "name": "JEFF",
                        "created_at": "2011-01-25T06:18:11Z",
                        "updated_at": "2011-01-25T06:20:24Z",
                        "store_id": 1,
                        "id": 1
                    }
                ],
                "email": "123@gmail.com"
            }
        ]
    }
    I get errors while using TPL to render the data. the WEIRDEST thing about it is that I can get one of the three hasMany to display it's data (which tells me they are configured properly, but cannot get the other 2 to show up.)

    And the WEIRDEST thing of all is that if I change up the ordering of my models in the index.html file I can predictably cause errors for the Deals model if it's not included first.

    My Xtemplate:
    Code:
        itemTpl: new Ext.XTemplate(
    			'<tpl for=".">',
    			 '<h1>{name}{records}</h1>',
                '<h2>{network}</h2>',
    		'<h2>{phone}</h2>',
    		'<h2>{email}</h2>',
    			'</tpl>',
    				'<tpl for="earns">',
    				 '<h1>{name}{records}</h1>',
    	            '<h2>{description}</h2>',
    				'</tpl>',
    				'<tpl for="deals">',
    				 '<h1>{name}{records}</h1>',
    	            '<h2>{description}</h2>',
    				'</tpl>',
    				'<tpl for="rewards">',
    				 '<h1>{name}{records}</h1>',
    	            '<h2>{description}</h2>',
    			'</tpl>',

  9. #9
    Touch Premium Member
    Join Date
    Jan 2011
    Posts
    37
    Vote Rating
    0
    gp40 is on a distinguished road

      0  

    Default


    I'm having the same issue and the work around does not seem to help. Looking forward to a solution/workaroud. Thx. /Greg

  10. #10
    Ext JS Premium Member
    Join Date
    Jun 2007
    Posts
    8
    Vote Rating
    0
    emilschutte is on a distinguished road

      0  

    Default


    edspencer's patch worked for me, but note you also need another method on Ext.data.Reader:

    Code:
    getAssociatedDataRoot: function(data, associationName) {
        return data[associationName];
    }
    I'm using 1.0.

Similar Threads

  1. Hint: Models with hasMany in Lists
    By NeoVanGoth in forum Sencha Touch 1.x: Discussion
    Replies: 3
    Last Post: 15 Jul 2011, 5:19 AM
  2. Replies: 12
    Last Post: 19 Jan 2010, 5:36 AM
  3. regarding the EXTJS trees - association/link
    By sravanth in forum Ext 3.x: Help & Discussion
    Replies: 3
    Last Post: 5 Jun 2009, 2:30 AM
  4. 1:m association tables
    By johnstontrav in forum Ext 2.x: Help & Discussion
    Replies: 13
    Last Post: 14 May 2008, 3:27 AM

Thread Participants: 12

Tags for this Thread

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