PDA

View Full Version : How to associate model to model with 1 to 1 relationshop



tomerb78
14 Mar 2012, 1:51 AM
Say I have 2 model types



Ext.define('APP.model.QueryData', {
extend: 'Ext.data.Model',
fields: [
{ name: 'Time', type: 'int' },
{name: 'Level', type: 'int' },
{name: 'TotalRequests', type: 'int' }]
}





Ext.define('APP.model.Server', {
extend: 'Ext.data.Model',
fields: [
{ name: 'Identifier', type: 'int' },
{name: 'IpAddress', type: 'int' },
{name: 'SslEnabled', type: 'boolean' }]
}



The data that come back from server is a QueryData entity which contains Server entity as a member (Only one).
I expect to get a model of QueryData that contains somehow only one model of Server.

Here is the a JSON for this data that come from the server:



{"QueryResult":{"Success":true,"Data":{"Time":25,"Level":1,"Server":{"Identifier":1,"IpAddress":"10.61.21.123","SslEnabled":false},"TotalRequests":10},"ErrorDetails":null}}


In documentation I saw only examples of one to many and many to one associations.
What is the correct way to to define this association?

Thanks.

mitchellsimoens
14 Mar 2012, 10:04 AM
4.1.0 has a hasOne association now.

dedoz
14 Mar 2012, 12:11 PM
if you are using 4.1 beta 1, 2 or 3, dont use hasOne association, its bugged i posted on discusion forum didnt had time to post to the bugs forums after mitchell almost hit me for posting in discussion forum (jokes)

dedoz
14 Mar 2012, 12:27 PM
anyway in a relation 1:1 you need to place a foreign key (thats not related to ExtJs).
where is your foreign key ?
in a 1:1 relation, you have to use belongTo or HasOne according to "where the foreign key is" and i cant see a foreign key in your models.

i cant even see the an idProperty set to your models.
so your models should like something like


Ext.define('APP.model.QueryData', {
extend: 'Ext.data.Model',
fields: [
{ name: 'Time', type: 'int' },
{name: 'Level', type: 'int' },
{name: 'TotalRequests', type: 'int' }
],
idProperty: 'Time' //tells to ExtJs this model primary key field
}

Ext.define('APP.model.Server', {
extend: 'Ext.data.Model',
fields: [
{ name: 'Identifier', type: 'int' },
{name: 'IpAddress', type: 'int' },
{name: 'SslEnabled', type: 'boolean' },
{name: 'Time_id', type: 'boolean' } // foreignKey that associates QueryData with Server model.
],
idProperty: 'Identifier' //tells to ExtJs this model primary key field
}

after that (if i placed the foreign key as you were expecting) then the associations should be


Ext.define('APP.model.QueryData', {
extend: 'Ext.data.Model',
fields: [
{ name: 'Time', type: 'int' },
{name: 'Level', type: 'int' },
{name: 'TotalRequests', type: 'int' }
],
hasOne : [
{ model:'APP.model.Server', foreignKey:'Time_id',
getterName: 'getServer', setterName : 'setServer'
associationKey : 'server'// response data key where server associated data comes
}
]
idProperty: 'Time'
}

Ext.define('APP.model.Server', {
extend: 'Ext.data.Model',
fields: [
{ name: 'Identifier', type: 'int' },
{name: 'IpAddress', type: 'int' },
{name: 'SslEnabled', type: 'boolean' },
{name: 'Time_id', type: 'boolean' }
],
belongsTo : [
{ model:'APP.model.QueryData', foreignKey:'Time_id',
getterName: 'getQuery', setterName : 'setQuery'
associationKey : 'query'// response data key where query associated data comes
}
],
idProperty: 'Identifier'
}

but remember not use hasOne :D

tomerb78
15 Mar 2012, 12:03 AM
anyway in a relation 1:1 you need to place a foreign key (thats not related to ExtJs).
where is your foreign key ?
in a 1:1 relation, you have to use belongTo or HasOne according to "where the foreign key is" and i cant see a foreign key in your models.

i cant even see the an idProperty set to your models.


Hi, thanks for your answer.

for your question, i don't want to use foreign key. these are not 2 seperated collections that i should mutch using keys. this is simple entity inside another entity. Look at the JSON response:



{"QueryResult":{"Success":true,"Data":{"Time":25,"Level":1,"Server":{"Identifier":1,"IpAddress":"10.61.21.123","SslEnabled":false},"TotalRequests":10},"ErrorDetails":null}}


Inside the "Data" obj i have a member "Server" that holds the server obj. I don't want to add another member to each to them to point one another... isn't that possible?