PDA

View Full Version : belongsTo Model Association Help



matt_d_rat
26 Jul 2011, 5:21 AM
I have been reading a lot of posts regarding associations and have gone through the docs extensively trying to work out how to use them. I think I have a good understanding of how to use them based on what I have read, however they seem to not be working.

Am I right in thinking that if I make a call for that "belongsTo" something, it should also return the associated data to its respective store as well?

Basically what I cam trying to achieve is to show the details of an invoice, of which one of the details is the name of the entity which the invoice "belongsTo". Below are my models, each of which map to separate stores of the same names. Would really appreciate any help anyone is able to offer as this has me a little confused.

Entity Model:


Ext.regModel("Entity", {
fields: [
{name: "id", type: "int"},
{name: "company_name", type: "string"},
{name: "building", type: "string"},
{name: "address1", type: "string"},
{name: "address2", type: "string"},
{name: "town", type: "string"},
{name: "county", type: "string"},
{name: "postcode", type: "string"},
{name: "phone1", type: "string"},
{name: "phone2", type: "string"},
{name: "fax", type: "string"},
{name: "website", type: "string"},
{name: "email", type: "string"},
{name: "contact_name", type: "string"},
{name: "date_modified", type: "date", dateFormat: "Y-m-d h:m:s" },
{name: "external_id", type: "int"},
{name: "statement_url", type: "string"},
{name: 'invoice', type: 'array'}
],

associations: [
{type: 'hasMany', model: 'Invoice', name: 'invoice'}
],

proxy: {
type: 'localstorage',
id: 'entity'
}
});


Invoice Model:


Ext.regModel("Invoice", {
fields: [
{name: "invoice_id", type: "int"},
{name: "dateCreated", type: "date", dateFormat: "Y-m-d H:i:s"},
{name: "dateDue", type: "date", dateFormat: "Y-m-d H:i:s"},
{name: "creditTerms", type: "int"},
{name: "description", type: "string"},
{name: "items", type: "array"},
{name: "entityId", type: "int"},
{name: "reference", type: "string"},
{name: "project", type: "int"},
{name: "status", type: "string"},
{name: "data_modified", type: "date", dateFormat: "Y-m-d H:i:s"},
{name: "invoice_prefix", type: "string"},
{name: "invoice_number", type: "string"},
{name: "exiternal_id", type: "int"},
{name: "statementPage", type: "string"}
],

associations: [
{type: 'belongsTo', model: 'Entity', associatedName: 'company_name', primaryKey: 'id', foreignKey: 'entityId'}
],

proxy: {
type: 'localstorage',
id: 'invoice'
}
});