PDA

View Full Version : Model and belongsTo relationship



YomeKitsuma
14 Feb 2012, 3:04 AM
Hi,

I face this problem :

I've a Model of User which have an OrganizationID.
Firstly, I want to associate (and display) OrganizationID to the Name of the Organization (which is an other Model).
Secondly, in my grid, when editing, i'll like to display a combo box (with the Organizations name).

Here is my code :

My model =

Ext.define('Admin.model.User',{ extend : 'Ext.data.Model',

idProperty : 'Id',
fields : [
{name:'Id', type:'integer'},
{name: 'mappedOrganizations', mapping: 'organization'},
{
name: 'OrganizationId',
type: 'string',
convert: function(value, record) {
var orgs = record.get('mappedOrganizations');
var val = 'unknown';
for (var i=0; i<orgs.length; i++) {
if (value == orgs[i].Id) {
val = orgs[i].Name;
break;
}
}
return val;
}
},
{name:'RoleId', type:'integer'},
'FirstName',
'LastName',
'UserName',
'Email',
'Phone',
{name:'Active', type:'boolean'},
{name:'Avatar', type:'string'},
{name:'base_path', defaultValue:Altair.desktop.Constants.USERS_AVATAR_PATH}
],
belongsTo: [{
model : 'Admin.model.Organization',
name : 'organization',
foreignKey : 'OrganizationId',
primaryKey : 'organization_id',
instanceName : 'OrganizationBelongsToInstance',
associationKey : 'organizationKey',
getterName : 'getOrganization'
}]
});

My store :

Ext.define('Admin.users.store.Users',{ extend : 'Ext.data.Store',

model : 'Admin.model.User',

proxy : {
type : 'ajax',
reader : 'users-reader',
url : 'data/admin/users.json',
autoLoad : true

}
});

My Reader (so simple)

Ext.define('Admin.users.reader.UsersReader', {
extend : 'Ext.data.reader.Json',

alias : 'reader.users-reader',
root: 'results.users'
});

Here my json data


{
success: true,
results: {
users: [{
Id : 1,
OrganizationId : 1,
RoleId : 1,
FirstName : "John",
LastName : "Doe",
UserName : "JohnDoe",
Email : "john.doe@osmose.fr",
Phone : "111-111-111",
Active : "true",
Avatar : "images/icons/48/user-icon.png",
base_path : ""
},
{
Id : 2,
OrganizationId : 1,
RoleId : 2,
FirstName : "Koungue",
LastName : "Foo",
UserName : "KoungueFoo",
Email : "koungue.foo@osmose.fr",
Phone : "111-111-111",
Active : "true",
Avatar : "images/icons/48/user-icon.png",
base_path : ""
},
{
Id : 3,
OrganizationId : 2,
RoleId : 1,
FirstName : "Supa",
LastName : "Dupa",
UserName : "SupaDupa",
Email : "supa.dupa@enoleo.fr",
Phone : "222-222-222",
Active : "true",
Avatar : "images/icons/48/user-icon.png",
base_path : ""
},
{
Id : 4,
OrganizationId : 3,
RoleId : 1,
FirstName : "Mista",
LastName : "Ti",
UserName : "MistaTi",
Email : "mista.ti@tagall.fr",
Phone : "333-333-333",
Active : "true",
Avatar : "images/icons/48/user-icon.png",
base_path : ""
}]
}
}


{
success: true,
organizations: [{
organization_id : 1,
Id : 1,
Name : "Tutu",
Email : "info@tutu.fr",
Phone : "0493",
Adress : "Roquefort-les-pins"
},
{
organization_id : 2,
Id : 2,
Name : "Tata",
Email : "info@tata.fr",
Phone : "0493",
Adress : "Roquefort-les-pins"
},
{
organization_id : 3,
Id : 3,
Name : "Titi",
Email : "info@titi.fr",
Phone : "0493",
Adress : "Roquefort-les-pins"
}]
}


So, when i'm tracing the result everything works well for the "Users data" but nothing is loaded in the Organizations association...

Does "belongsTo" really works in 4.0.7 ?

ps : i'va also try to put "organizations data" into the users.json... nothing works

mitchellsimoens
14 Feb 2012, 5:32 AM
When you do the getOrganization, is there any call made in the Network tab?

YomeKitsuma
14 Feb 2012, 5:43 AM
Ok, all my code was false - i've read carefully the docs and change all of my lines code... but i still get a strange exception onto the 'model'

so, now here is my new code :


Ext.define('Admin.model.User',{ extend : 'Ext.data.Model',

idProperty : 'Id',
fields : [
{name:'Id', type:'int'},
{name: 'OrganizationId', type: 'int'},
{name:'RoleId', type:'int'},
'FirstName',
'LastName',
'UserName',
'Email',
'Phone',
{name:'Active', type:'bool'},
{name:'Avatar', type:'string'},
{name:'base_path', defaultValue:Altair.desktop.Constants.USERS_AVATAR_PATH}
],
belongsTo: [{
model : 'Admin.model.Organization',
name : 'organization',
getterName : 'getOrganization',
foreignKey : 'OrganizationId',
primaryKey : 'Id'
}]
});

and for my Organization


Ext.define('Admin.model.Organization',{ extend : 'Ext.data.Model',


idProperty : 'Id',
fields : [
{name:'Id', type:'int'},
'Name',
'Email',
'Phone',
'Adress'
]
});

And now two thing appends :

1) if I keep it like this i've got the error


You are using a ServerProxy but have not supplied it with a url.

this hapent when i try to "convert" the OrganizationId in the User model


{name: 'OrganizationId', type: 'int', convert : function(value, record) {
console.log('> convert = ' + value + ' / ' + record);
record.getOrganization({
success : function(item, operation) {
console.log(' - ' + item.get('Name'));
},
scope : this
});
}
},

2) if I add (like the error said) a proxy to my Organization model


Ext.define('Admin.model.Organization',{ extend : 'Ext.data.Model',


idProperty : 'Id',
fields : [
{name:'Id', type:'int'},
'Name',
'Email',
'Phone',
'Adress'
],

proxy : {
type : 'ajax',
reader : 'organization-reader',
url : 'data/admin/organizations.json',
//autoLoad: true
}
});

I've got only the first item in my organization.json data (which is "tutu"), even if the OrganizationId change for my users...

...i'm about to be close, i've spent more than 2 days on this, this begin to make me...hum...sick =D

dedoz
14 Feb 2012, 12:17 PM
"this hapent when i try to "convert" the OrganizationId in the User model"
what that means?


i guess you define a User and Organization model,
then set an association User belongs to Orgnization.
and then ?
you do User.load() ?
what are you sending as response for User.load() ?
do you include the associated data ? (organization) ?


or u do
u = user.load(something)
u.getOrganization() and here you get the error ?


check the data sent and received from your browser to the server and in reverse.


if you dont send (in response) the Organization data in response to a User.load(), doing .getOrganization will ask you to provide a proxy because that organization data is not there, so needs to load from the server.


if you are loading the user's organization data separately, doing user.getOrganization() then chek what ExtJs is sending to the server as request.

YomeKitsuma
14 Feb 2012, 11:41 PM
Hi Dedoz,

The call to the "organization" seems to be well done. In my "Network" view (i.e. chrome console) i can see the load of required data (organization).


{ success: true,
results : {
organizations: [{
Id : 1,
Name : "Tutu",
Email : "info@tutu.fr",
Phone : "0493",
Adress : "Roquefort-les-pins"
},
{
Id : 2,
Name : "Tata",
Email : "info@tata.fr",
Phone : "0493",
Adress : "Roquefort-les-pins"
},
{
Id : 3,
Name : "Titi",
Email : "info@titi.fr",
Phone : "0493",
Adress : "Roquefort-les-pins"
}]
}
}

Thing is that the received data only gives the "first" item (Id = 1, in red there) of the list (which is a 3 sets of organization, as shown in my json organization file), even if the OrganizationId in user record is 2,3,...

I suppose, since i do not use a "real" DB, but a flat file, that the Id passed through the request (when User calls its associated organization data) is not taken in consideration.

I've also try to "listen" the load in my User's store... getOrganization().count return 0 items.

Maybe it will be better to give a simple sample of what i'm doing...

I've spent (too much) time only to display associated data, and i really do not want to "inject" organization data in the user "node" of my json like :


users: [{ Id : 1,
OrganizationId : 1,
RoleId : 1,
FirstName : "John",
LastName : "Doe",
UserName : "JohnDoe",
Email : "john.doe@osmose.fr",
Phone : "111-111-111",
Active : "true",
Avatar : "images/icons/48/user-icon.png",
base_path : "",
organizations: [{
Id : 1,
Name : "Tutu",
Email : "info@tutu.fr",
Phone : "0493",
Adress : "Roquefort-les-pins"
},
{
Id : 2,
Name : "Tata",
Email : "info@tata.fr",
Phone : "0493",
Adress : "Roquefort-les-pins"
},
{
Id : 3,
Name : "Titi",
Email : "info@titi.fr",
Phone : "0493",
Adress : "Roquefort-les-pins"
}]
}

simply because it makes "non-sens" due to the "belongsTo" feature in ExtJs 4 ;-)

Thanks for reply...

YomeKitsuma
15 Feb 2012, 1:01 AM
Thing is that the received data only gives the "first" item (Id = 1, in red there) of the list (which is a 3 sets of organization, as shown in my json organization file), even if the OrganizationId in user record is 2,3,...

I suppose, since i do not use a "real" DB, but a flat file, that the Id passed through the request (when User calls its associated organization data) is not taken in consideration.


...after tracing and checking the "Query String Parameters" in the chrome console it is clear that the call onto a flat file is the reason of this !

This will be (surely) ok when we go onto a real DB/Server !


And I take attention to the thing that's "request are asynchronously", so, i've added a callback, and now all thing goes well.

Thanks Dedoz to having pointed out this important thing "asynchronously call" !!