Relational-Notes
Archived from the Xataface Users forum.
Aoirthoir — Wed Jul 19, 2006 8:15 am
What are Relational-Level Notes?
A relational note system allows us to enter notes on any table, or child tables which can then be viewed by the parent table.
So for instance I have several tables:
CUSTOMER_PERSON ( an individual person’s information )
CUSTOMER_ADDRESS ( since many customers have multiple addresses )
CUSTOMER_PHONE ( since many customers have multiple phone numbers )
Now for any customer I will probably want notes to be taken on different dates. So I have a notes table set up just for that. However, if I enter address information, I might want to detail notes on that, for instance why I am using this, why I added the address, why I changed it and so forth. While I have a comments field in every table, I want notes as a seperate module because I wish to have a running, dated record, of what has been done.
So if I enter notes on an address, I wish to also, at the Customer level, be able to see those notes. Well that is exactly what the Relational-Notes system does. It allows us to go into a customer, and see all the notes entered on any of the Customer modules. Also it lets us go into a module ( Phones, Addresses, Orders etc ) and see notes specific to just that module.
In dataface this is very very VERY easy to accomplish. Here is how.
First, in every table I have an ID_RECORD field. ( All of my ID_ fields tell me this is table specific information and not needed by the user ). If there is ANY chance that the table will be a child table to another table, I also place a PARENT_ID_RECORD field in it. This of course then is used to relate to the parent. For the notes table I have an additional field GRANDPARENT_ID_RECORD.
Thus if I create a note in the customer record, GRANDPARENT_ID_RECORD is set to the ID_RECORD of the customer. If I create a note in a module ( addresses, phones ) then GRANDPARENT_ID_RECORD of the note table is set to PARENT_ID_RECORD of the module table ( address ) and PARENT_ID_RECORD of the note table is set to ID_RECORD of the module table ( address ).
Lastly, since modules might end up having the same value in ID_RECORD ( an address with a value 12 and a phone with a value 12 ) I set up a field to let me know which module ( table ) this note is for. I call it PARENT_TABLE_NAME and in dataface I just put a text value. ( I got this idea from Mr. Steve Hannah the Dataface creator see here for his examples: [url]http://framework.weblite.ca/forum/dataface-users/86[/url].
So that is all. Here is the dataface code and my table layouts:
- Code: Select all
- `` CREATE TABLE
CUSTOMER_PERSON(
ID_RECORDmediumint( 8 ) unsigned zerofill NOT NULL auto_increment COMMENT ‘Unique RecordID. Auto Incremented. Used for Table Relationships and to identify specific record.’,
ID_CHANGEDtimestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT ‘Date record was added or changed’,
NAME_FIRSTvarchar( 100 ) NOT NULL COMMENT ‘Customer’’s first name.’,
NAME_MIDDLE_INITIALvarchar( 1 ) default NULL COMMENT ‘Middle initial.’,
NAME_LASTvarchar( 100 ) NOT NULL COMMENT ‘Customer’’s last name.’,
LINK_LIST_TYPE_NAME_TITLEvarchar( 20 ) default NULL COMMENT ‘Links to name title table.’,
LINK_LIST_TYPE_NAME_SUFFIXvarchar( 20 ) default NULL COMMENT ‘links to name suffix table.’,
SSN_3smallint( 3 ) unsigned zerofill default NULL COMMENT ‘First three digits of ssn.’,
SSN_2smallint( 2 ) unsigned zerofill default NULL COMMENT ‘Middle two digits of ssn.’,
SSN_4smallint( 4 ) unsigned zerofill default NULL COMMENT ‘Last four digits of ssn.’,
DATE_BIRTHdate default NULL COMMENT ‘Birth date.’,
GENDERenum( ‘Female’,’Male’,’Intersexed’ ) default NULL COMMENT ‘Gender.’,
HEIGHTvarchar( 20 ) default NULL COMMENT ‘Height.’,
WEIGHTvarchar( 20 ) default NULL COMMENT ‘Weight.’,
EMAILvarchar( 100 ) default NULL COMMENT ‘Customer’’s email address.’,
COMMENTtext COMMENT ‘Usually a tiny box for typing freeform comments.’,
PRIMARY KEY (ID_RECORD)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT=’Personal information on the Customer.’CREATE TABLE
CUSTOMER_ADDRESS(
ID_RECORDmediumint( 8 ) unsigned zerofill NOT NULL auto_increment COMMENT ‘Unique RecordID. Auto Incremented. Used for Table Relationships and to identify specific record.’,
ID_CHANGEDtimestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT ‘Date record was added or changed’,
PARENT_ID_RECORDmediumint( 8 ) unsigned zerofill NOT NULL COMMENT ‘This is the same as ID_RECORDID of the calling table in a relationship. In this table it is not unique. Used for identification and somewhat dynamic relationships.’,
LINK_LIST_TYPE_DELIVERYvarchar( 20 ) default NULL COMMENT ‘Links to the Delivery Type Table. Examples UPS/DHL/Postal etc.’,
ADDRESS_LINE1varchar( 100 ) NOT NULL COMMENT ‘Usually the street address’,
ADDRESS_LINE2varchar( 100 ) default NULL COMMENT ‘Generally a Suite, Box, Block etc.’,
CITYvarchar( 100 ) NOT NULL COMMENT ‘City’,
LINK_LIST_STANDARD_USA_STATE_NAMESchar( 2 ) NOT NULL COMMENT ‘Links to the State Abbreviation/Name Table’,
ZIP_5mediumint( 5 ) unsigned zerofill NOT NULL COMMENT ‘Zip Code ( Will eventually link to a Zip code table’,
ZIP_4smallint( 4 ) unsigned zerofill default NULL COMMENT ‘Plus Four of the Zip’,
DATE_RANGE_DELIVER_BEGINdate default NULL COMMENT ‘Date to start using this address’,
DATE_RANGE_DELIVER_ENDdate default NULL COMMENT ‘Date to cease using this address’,
COMMENTtext COMMENT ‘Usually a tiny box for typing freeform comments.’,
PRIMARY KEY (ID_RECORD)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT=’Customer’’s Address and Delivery Info’CREATE TABLE
CUSTOMER_PHONE(
ID_RECORDmediumint( 8 ) unsigned zerofill NOT NULL auto_increment COMMENT ‘Unique RecordID. Auto Incremented. Used for Table Relationships and to identify specific record.’,
ID_CHANGEDtimestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT ‘Date record was added or changed’,
PARENT_ID_RECORDmediumint( 8 ) unsigned zerofill NOT NULL COMMENT ‘This is the same as ID_RECORDID of the calling table in a relationship. In this table it is not unique. Used for identification and somewhat dynamic relationships.’,
LINK_LIST_TYPE_PHONEvarchar( 20 ) NOT NULL COMMENT ‘This links to the TYPE_PHONE table. ( Examples Cell/Home/Fax etc )’,
AREA_CODEsmallint( 3 ) unsigned zerofill NOT NULL COMMENT ‘Area code’,
PREFIXsmallint( 3 ) unsigned zerofill NOT NULL COMMENT ‘First Three digits after the area code.’,
SUFFIXsmallint( 4 ) unsigned zerofill NOT NULL COMMENT ‘Last four digits of the number,’,
EXTENSIONsmallint( 4 ) unsigned zerofill default NULL COMMENT ‘Extension’,
COMMENTtext COMMENT ‘Usually a tiny box for typing freeform comments.’,
PRIMARY KEY (ID_RECORD)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT=’Phone info on the Customer’CREATE TABLE
CUSTOMER_NOTE(
ID_RECORDmediumint( 8 ) unsigned zerofill NOT NULL auto_increment COMMENT ‘Unique RecordID. Auto Incremented. Used for Table Relationships and to identify specific record.’,
ID_CHANGEDtimestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT ‘Date record was added or changed’,
GRANDPARENT_ID_RECORDmediumint( 8 ) unsigned zerofill NOT NULL COMMENT ‘This is the same as PARENT_ID_RECORDID of the calling table in a relationship. In this table it is not unique. Used for identification and somewhat dynamic relationships.’,
PARENT_ID_RECORDmediumint( 8 ) unsigned zerofill default NULL COMMENT ‘This is the same as ID_RECORDID of the calling table in a relationship. In this table it is not unique. Used for identification and somewhat dynamic relationships.’,
PARENT_TABLE_NAMEvarchar( 50 ) default NULL COMMENT ‘This is the name of the calling table.’,
COMMENTtext COMMENT ‘Usually a tiny box for typing freeform comments.’,
PRIMARY KEY (ID_RECORD)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT=’Notes on the Customer and any child tables’ ``
Now the dataface code:
tables/CUSTOMER_PERSON/relationships.ini
- Code: Select all
[CUSTOMER_NOTE] CUSTOMER_NOTE.GRANDPARENT_ID_RECORD = "$ID_RECORD"
tables/CUSTOMER_ADDRESS/relationships.ini
- Code: Select all
[CUSTOMER_NOTE] __sql__ = "select * from CUSTOMER_NOTE where GRANDPARENT_ID_RECORD='$PARENT_ID_RECORD' and PARENT_ID_RECORD='$ID_RECORD' and PARENT_TABLE_NAME='CUSTOMER_ADDRESS'"
tables/CUSTOMER_PHONE/relationships.ini
- Code: Select all
[CUSTOMER_NOTE] __sql__ = "select * from CUSTOMER_NOTE where GRANDPARENT_ID_RECORD='$PARENT_ID_RECORD' and PARENT_ID_RECORD='$ID_RECORD' and PARENT_TABLE_NAME='CUSTOMER_PHONE'"
Or any table really:
tables/TABLE_NAME/relationships.ini
- Code: Select all
[NOTE_TABLE_NAME] __sql__ = "select * from NOTE_TABLE_NAME where GRANDPARENT_ID_RECORD='$PARENT_ID_RECORD' and PARENT_ID_RECORD='$ID_RECORD' and PARENT_TABLE_NAME='Table name ( or any descriptor you choose as long as it is consistent for one table and DIFFERENT from all other tables or you WILL get errors'"