MySQL Forums
Forum List  »  PHP

Logical Inheritance in MySQL
Posted by: mlerch
Date: December 17, 2005 07:33AM

I have been boggling my mind with this, and I have found a couple of solutions, but they seem a bit overkill and slow - so I am hoping someone here could help out.

I am trying to build a MySQL database to coincide with PHP class structures (and don't suggest I just not do this - because I am... too bad :-P )

Take, for instance, a situation where I have three tables... I'll call them table_a, table_b, and table_c. I want it to be set up that table_c inherits (like a child object) information from table_b, which does the same from table_a.

Theoretically this is how it would work. Here is the table structure.

Table A has 2 fields. "id" and "name"
Table B has 4 fields "id" "name" "parent" and "comment"
Table C has 4 fields "id" "name" "parent" and "value"

Now, if I set it this way:

A.id = 1;
A.name = "A";
B.id = 1;
B.name = "B";
B.parent = "A";
B.comment = "Our comment";
C.id = 1;
C.name = "C";
C.parent = "B";
C.value = "Data stuff";

Now, when I instantiate C (ie I pull data from C) I want to have it first pull data from A, then from B, then from C. When it pulls the information from B, and there is overlap (B.name and A.name for instance) B should win out and I would get a response back with B.name. Next it would pull C and do that same. If there is not overlap, or there is a blank/non-existent field, it would not overwrite. So, if I made C, I SHOULD have the following:

id = 1
name = "C"
parent = "B"
comment = "Our comment"
value = "Data stuff"


Now, I have done this a few ways. For instance I could do a join twice, and have PHP dig through a mysql_fetch_array result and find the last instance of each variable. The other option I had was to use SELECT CASE repeatedly for each field of the table, and have it do the logic.. but if I say made children of children of children (multiple inheritance) those case statements would get to be pages long.

There HAS to be a better way that I am just not thinking of... any help?

Options: ReplyQuote


Subject
Written By
Posted
Logical Inheritance in MySQL
December 17, 2005 07:33AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.