View Full Version : Polimorphic table inheritance and models}

22 Jan 2012, 7:26 AM

I'm running into something which not covered by the very sparse Assosiations part of Ext.Data.Model.

Let me explain with an simple example:

In my back-end:

(Very simplified explanation model, it does not reflect project)

I have a Table called Address
Next I have a table with EmailAddress, which inherits Address.

My Back-End supports polymorphic tables; This ends up as when I create an email address
" record, the data is split into the physical tables Address and EmailAddress Both with the same Id value and on the Address table the field "discriminator" is set to "EmailAddress"

So when I create EmailAddress(Value="me@somedomain.com") I get the following data:

Id : 1 (Integer, auto sequence, primary key)
discriminator : "EmailAddress" (String, set automatically by back-end)

(Email Address)
EmailAddress : 1 (Integer, foreignKey('Address.Id')
Value : "Me@somedomain.com"

If I query Addresses, I get 1 result [{'Id' : 1, 'discriminator' : 'EmailAddress', 'Value' : 'Me@somedomain.com'}]

So In this case the Objecttype returned is EmailAddress.

If Next make PhoneNumber (Extends Address too)

And make PhoneNumer(Value = '+31 123 45556')

I get:
Id :2
discriminator : 'PhoneNumber'

Id :2
Number : '+31 123 45556''

If I query Address for all:
[{'Id' : 1, 'discriminator' : 'EmailAddress', 'Value' : 'Me@somedomain.com'},
{'Id' : 2, 'discriminator' : 'PhoneNumber', 'Number' : '+31 123 45556'}]

If I query EmailAdddress for all:
[{'Id' : 1, 'discriminator' : 'EmailAddress', 'Value' : 'Me@somedomain.com'}]

If I query PhoneNumber for all:
[{'Id' : 2, 'discriminator' : 'PhoneNumber', 'Number' : '+31 123 45556'}]

Now If I want to reflect data In ExtJs I would need to Create a Ext.Data.Model for Address with Id and discriminator. Next I need Email Address and PhoneNumber to Extend from Address. with the proper IdProperty and Id set as "BelongsTo". and the specific fields added to the fieldslist Inherited from Address.

Address should have a relation setup Between Id and the Id of its "children".

I do not worry about handling data in ExtJs from the query Address all (Mixed record data) just jet.
I can Override stuff from Ext.data.Model and/ or Ext.data.Store to get THAT working later.
(I think I have to replace objects based on the "discriminator" in the Store.Onload from the raw data:
Something like

For Each Record:
if (Record.discriminator == 'EmailAddress'){
Record = new EmailAddress()
// and load the data from RawData based on Id

Help on this would be nice but as said at the moment it is not that important)

I am working on generating Model and Store Javacript code based on my Objects in my back-end (So objects can generate their own models and Stores). Making these by hand is a tedious task for databases with hundreds of tables, not to speak about tracking changes etc.

People Interested in knowing about the back-end:
Database back-end: SQLAlchemy
Databases either one of: Postgress, Oracle, MySQL, MSSql, SQLlite etc., etc.
Communicates Data with Ext.Direct (Automated set-up).

Any help, tips etc. are highly appreciated, I'm much more of a Python guy and I am having a hard time with ExtJs there is so much technical documentation missing and the ExtJs Sources do not always clarify things enough without digging deeper and deeper.

I also wander if this is something I would spend our support credits on


Edit: I have found out that ExtJs, when loading data, ignores fields not defined in the store's model, So I assume I can extend/overwrite Store to handle the changing model. I have to look into the raw data more closely if it reflects changes or not.