Hi all,
I'm having problems designing an app. My problem lies with proper object representation of data and interfacing with a database.
The system is under development and using non-oo techniques is not possible.
Many features of the program require mapping a record in one table a, to records in other tables b, c etc.
IE, in the same script:
1. Insert record into table a.
2. Link record in table a to record in table b, c etc.
The problem I'm facing is how to use objects to represent records in these tables without redudency. Redundency meaning in this case variables not represented inside objects by their actual value and instead be created on the fly when the object is inserted into the database table.
Example: Object A has $ID as a field. Instead of being able to initialize it on object creation with some value x, having to have $ID = "" and then the id being created and initialised by the auto_increment property on insertion into the database.
Why is this a problem in my case? Because i need to minimise query numbers to the database as much as possible while still providing the easy interface that objects offer.
A->insertTableA() //insert object A's data into table a
A->insertTableB() //insert object A's other data into table b, (record in table a and b will be linked by a unique id)
A->getId() //get the ID field of the object.
Problem: If A is created in same script as insert() AND getId() will be used a problem is ecountered. How do I get $ID field using getID() as $ID = "" and as such obviously does not match the value in the auto_increment column in table A. I therefore cannot link the record in Table A to the record in Table B without using another query to find out what the ID in table A is so i can the insery it into table B along with other data and therefore create a link.
Representing an object in this way = crap design. Performing as many querys as this = crap performance/resource usage.
I'm currently performing the query to get the ID from inside the object. Hiding this process from the programmer. This way in my opinion is the sellotape and bubblegum method.
I have decided that making sure each object has a field that must be unique and then using hash(uniquefield) to grab a unique alphanumeric id for that object. However, using an alphanumeric key as an Id sheds the ability to gain performance from numeric id representation (when performing select from.... where ID = ... querys)
Having a unique field for each object really removes the neccessity for having an ID, however, if all the fields are very long, the hash(field) will be long and slow to compare in querys and so would the plain old compare with field.
Can anyone offer me suggestions of how to get around this problem. Like maybe getting a unique numeric id from a unique alphanumeric field.
Thanks for reading this incredibly long question (it got away from me a bit ;-) )
Billy