PDA

View Full Version : Understanding hasMany and belongsTo.



incutonez
24 Oct 2013, 9:18 AM
Hello all. I've been struggling to understand how to use hasMany and belongsTo for quite sometime. My understanding is hasMany is a 1:many relationship and belongsTo is a many:1 relationship, so does that imply that if you have a hasMany relationship, a belongsTo is needed in its child model? I've read several articles on it:


Loading Nested Data on the Ext.data.reader.Reader (http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.reader.Reader) page.
Ext.data.association.BelongsTo (http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.association.BelongsTo)
Ext.data.association.HasMany (http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.association.HasMany)
StackOverflow question (http://stackoverflow.com/questions/16482819/association-sample-in-extjs-4-2)
ExtJS Tutorials (http://extjs-tutorials.blogspot.ca/2012/05/extjs-hasmany-relationships-rules.html) - HasMany article
ExtJS Tutorials (http://extjs-tutorials.blogspot.ca/2012/05/extjs-belongsto-association-rules.html) - BelongsTo article
hasMany vs belongsTo (http://programmers.stackexchange.com/questions/152731/what-is-the-main-difference-between-has-many-and-belongs-to-relationship-in-mysq)

Still a bit confused though. Let's say I have the following data:


var data = {
"config": {
"name": "blah",
"id": 1,
"someconfig": [{
"name": "Services", "tabs": [{
"id": 0, "name": "Details", "layout": "hbox"
}, {
"id": 1, "name": "Sources", "layout": "hbox"
}, {
"id": 2, "name": "Paths", "layout": "hbox"
}, {
"id": 3, "name": "Ports", "layout": "hbox"
}, {
"id": 4, "name": "Levels", "layout": "hbox"
}, {
"id": 5, "name": "Notes", "layout": "hbox"
}]
}, {
"name": "Services2", "tabs": [{}]
}]
}
};

I would create a model for config:


Ext.define('Config', {
extend: 'Ext.data.Model',
fields: ['name'],
hasMany: [{
name: 'someconfig',
model: 'Someconfig',
associationKey: 'someconfig'
}],
proxy: {
type: 'memory',
data: data,
reader: {
type: 'json',
root: 'config'
}
}
});

So Config can have many Someconfigs because in the data, someconfig is an array of objects. Here's the Someconfig model:


Ext.define('Someconfig', {
extend: 'Ext.data.Model',
fields: [
'name'
],
hasMany: [{
name: 'tabs',
model: 'Tabs',
associationKey: 'tabs'
}]
});

Ok, same deal. Someconfig can have many Tabs because in the data, tabs is an array of objects. Here's the Tabs model:


Ext.define('Tabs', {
extend: 'Ext.data.Model',
fields: [
'id',
'name',
'layout'
],
belongsTo: [{
name: 'tabs',
model: 'Someconfig',
associationKey: 'tabs'
}]
});

Now, belongsTo is in there because I was messing around with this property. Regardless, I can't access Tabs from Someconfig, but I can access Someconfig from Config. Take a look at this code:


Config.load(1, {
success: function(record, operation) {
console.log(record.get('name')); // works
console.log(record.someconfig().getCount()); // works, gives me 2
console.log(record.someconfig().data.items[0].data); // only prints out name and config_id
// console.log(record.someconfig().tabs()); // doesn't exist
}
});

jsFiddle: demo (http://jsfiddle.net/incutonez/3q3uL/)

What I want to know is, shouldn't I be able to access tabs() from someconfig(), or am I misunderstanding the relationships? If it's the former, how would I fix my code?

incutonez
24 Oct 2013, 10:21 AM
Ah, it looks like I need to access the record, and then I have access to the tabs function, so something like this
console.log(record.someconfig().getAt(0).tabs()); // returns the tabs for record 0, which makes sense... just stupidity on my part.

incutonez
24 Oct 2013, 11:46 AM
The other curious thing is, if I set a belongsTo on the Config model, like so:


belongsTo: [{
model: 'Someconfig',
name: 'Someconfig',
associationKey: 'someconfig'
}]

When I do getSomeconfig() in the success call, I get back my someconfig object, but .data just contains an object with name="", but if I do .raw instead, I get my actual someconfig object... why is data empty, but raw has the actual data?

incutonez
31 Oct 2013, 5:53 AM
So what's interesting about this is, someconfig does not show in the data of record... I can only access someconfig by calling its method. This does not make sense to me... if the config model hasMany someconfig models, it should show up in config as an array of models!