PDA

View Full Version : models associations



temp05
19 Aug 2011, 12:54 PM
hi.
i have 3 mysql tables:
---
a
id|name

b
id|name

a_b
a_id|b_id
---

i create 3 models:


Ext.define('a', {
extend: 'Ext.data.Model',
fields: ['id', 'name']
});
Ext.define('b', {
extend: 'Ext.data.Model',
fields: ['id', 'name']
});
Ext.define('a_b', {
extend: 'Ext.data.Model',
fields: ['a_id', 'b_id']
});


i need to get all "b" for model "a" defined in the model "a_b", and i need to get all "a" for model "b" defined in the model "a_b"

please help

tobiu
20 Aug 2011, 7:22 AM
http://docs.sencha.com/ext-js/4-0/#/api/Ext.data.HasManyAssociation

is what you are looking for.

temp05
20 Aug 2011, 8:38 AM
I followed the instructions but I did not succeed.

now my models:



Ext.define('a', {
extend: 'Ext.data.Model',
fields: ['id', 'name'],
associations: [
{type: 'hasMany', model: 'a_b', name: 'bs', foreignKey: 'a_id',},
]
});
Ext.define('b', {
extend: 'Ext.data.Model',
fields: ['id', 'name'],
associations: [
{type: 'hasMany', model: 'a_b', name: 'as', foreignKey: 'b_id',},
]
});
Ext.define('a_b', {
extend: 'Ext.data.Model',
fields: ['a_id', 'b_id']
});





please help with code

christocracy
22 Aug 2011, 5:07 AM
You seem to be doing fine.

Now you'll want to define a #proxy for each of your models.



Ext.define('a', {
extend: 'Ext.data.Model',
fields: ['id', 'name'],
proxy: {
type: 'ajax',
url: '/a.json',
reader: {
type: 'json'
}
},
associations: [
{type: 'hasMany', model: 'a_b', name: 'bs', foreignKey: 'a_id',},
]
});
Ext.define('b', {
proxy: {
type: 'ajax',
url: '/b.json',
reader: {
type: 'json'
}
},
extend: 'Ext.data.Model',
fields: ['id', 'name'],
associations: [
{type: 'hasMany', model: 'a_b', name: 'as', foreignKey: 'b_id',},
]
});
Ext.define('a_b', {
proxy: {
type: 'ajax',
url: '/a_b.json',
reader: {
type: 'json'
}
},
extend: 'Ext.data.Model',
fields: ['a_id', 'b_id']
});

Berzzzebub
25 Aug 2011, 12:53 AM
Also remove commas at the ends of association strings. It's just wrong.