PDA

View Full Version : Problem with model associations (belongsTo and hasMany)



jmendez88
21 Oct 2013, 2:22 AM
I have a problem with association models. I have defined a model called "Usuario" and another model called "Grupos".

The model "Usuario" has a belongsTo association with "Grupo", and the model "Grupo" has a hasManyAssociation with "Usuario".

I also have two stores (Usuarios and Grupos), and right now, both of them have the information in their data properties.

Models



Ext.define('Mendez.model.Usuario', {
extend: 'Ext.data.Model',


requires: [
"Ext.data.UuidGenerator"
],




identifier: { type: 'uuid' },




fields: [
{ name: "id", type: 'int' },
{ name: "grupo_id", type: 'int' },
'nombre',
'email',
'telefono'
],


validations: [
{type: 'presence', field: 'id'},
{type: 'presence', field: 'grupo_id'},
{type: 'presence', field: 'nombre'},
{type: 'presence', field: 'email'},
{type: 'format', field: 'email', matcher: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/}


],


associations: [
{
type: 'belongsTo',
model: 'Mendez.model.Grupo',
primaryKey: 'id',
foreignKey: 'grupo_id',
getterName: 'getGrupo',
setterName: 'setGrupo'
}
],




proxy: {
type: 'localstorage',
id : 'Mendez-Usuario'
}


});



Ext.define('Mendez.model.Grupo', { extend: 'Ext.data.Model',


requires: [
"Ext.data.UuidGenerator"
],




identifier: { type: 'uuid' },


fields: [
{ name: "id", type: 'int' },
'nombre'
],


validations: [
{type: 'presence', field: 'id'},
{type: 'presence', field: 'nombre'},
],


associations: [
{
type: 'hasMany',
model: 'Mendez.model.Usuario',
primaryKey: 'id',
foreignKey: 'grupo_id',
name: 'usuarios'
}
],




proxy: {
type: 'localstorage',
id : 'Mendez-Grupo'
}


});

Stores


Ext.define('Mendez.store.Usuarios', { extend: 'Ext.data.Store',


requires: ['Mendez.model.Usuario'],


model: 'Mendez.model.Usuario',


autoload: true,


data: [
{id: 1, nombre: 'J', email: 'j@j.com', telefono: '000 000 000', grupo_id: 1},
{id: 2, nombre: 'M', email: 'm@m.com', telefono: '000 000 000', grupo_id: 2},
{id: 3, nombre: 'D', email: 'd@d.com', telefono: '000 000 000', grupo_id: 8}
]
});





Ext.define('Mendez.store.Grupos', { extend: 'Ext.data.Store',


requires: ['Mendez.model.Grupo'],


model: 'Mendez.model.Grupo',


autoload: true,


data: [
{id: 1, nombre: 'Admin'},
{id: 2, nombre: 'Customer'},
{id: 8, nombre: 'Other'}
]
});




If I execute the next code, it doesn't work right.



var usuariosStore = Ext.getStore("Usuarios");
var user = usuariosStore.last();
user.getGrupo().get("id"); // the result is right -> 8
user.getGrupo().get("nombre"); // the result is always ""

var gruposStore = Ext.getStore("Grupos");
var group = gruposStore.last();
group.usuarios(); // the result is always an empty array


Could someone show me what I am doing wrong?

Thanks.

ettavolt
21 Oct 2013, 7:20 AM
belongsTo returns empty record with id set, until it fetches the associated record. Fetch is started bu calling getter. You can return embedded object from server to initialize association immediately. There is no caching - I mean if you have loaded this record in a different way association won't find it.

jmendez88
21 Oct 2013, 7:40 AM
Thank you very much.

I will try it with a rest proxy.