Page 1 of 1

need help on oop database problem

Posted: Wed Feb 16, 2005 8:06 am
by dannymc1983
Hi, ive run into a problem designing a database (php and mysql) for submitting student projects. ive basically created 4 tables:
1) holds the project details eg. project title, project outline etc
students can pick from these projects
2) holds the project owners, eg when a project is picked its owner is entered into this table showing that the project has been picked and which student has picked the project and has a foreign key pointing to project details
3) holds the project files which the student will submit for a project and has a foreign key pointing to project details
4) holds the project grades which a lecturer will assign to a student for a
project and has a foreign key pointing to project details

ive decided to create a class for each table ( i assume this is the correct way of doing it???) . for example the class project will have methods getProjectName(), createProject() etc. while the class projectgrades will have methods deleteFile(), viewFile() etc.
now due to the hierarchial design of my tables ive decided to implement the classes in a similar fashion
as a result of the relationship between the classes i thought it best to use inheritance eg. the projectowner class inherits the projectDetails class and likewise with the projectgrades class and projectfiles class.
now i have also created a database operations class that performs all of the mysql querys eg, inserting, updating removing table data. but the problem arises when i try to implement this dataopertions class. what id like to know is how exactly do i implement this class in each of the classes ive named above in order to manipulate the database tables??? im failrly sure i dont use inheritance because the database opertions class doenst model a real world object. all i want to know is how does the class for each table make use of the services of the database operations class, what would be the best way of doing this???
any help on this would be much appreciated. its driving me mad!! :x

Posted: Wed Feb 16, 2005 8:25 am
by Maugrim_The_Reaper
There must be a better way than what I'll suggest - since this uses globals...

In the file where the project classes are instantiated (usually by instantiating he final inheriting class), you also instantiate the database operations class.

Now make the object name for the database operations class, e.g. $db, a global variable within the project classes' functions using the typical

global $db;

Within the project class methods, you then use the database object as normal: $db->method()....

This does work, it also means making the database object global within other classes to use it. I'm not an OOP expert, but that just seems messy to me - so I'll assume there's a tidier way...

Alternatively, add the database methods into the project class - if they are solely for use on these projects. May not be a true categorical grouping - but it'll work.

Also one class for all the project methods is just as valid as one class per project table...

Posted: Wed Feb 16, 2005 9:07 am
by feyd
danny, do not cross post again.

need help on an oop problem

Posted: Wed Feb 16, 2005 9:51 am
by dannymc1983
thanks for reply. I had been thinking about using something like what you were saying with the global object but i think for good oop the only files that should use the database operations class are those with classes relating to the table manipulaion eg. the project or grades class.
as for inserting the sql code directly into the project classes it would mean a lot of extra code and just seems a bit unnecessary, like there would be a lot of identical code in each class.
would you have any other suggestions or anyone else have any other suggestions???

Re: need help on oop database problem

Posted: Wed Feb 16, 2005 10:45 am
by BDKR
dannymc1983 wrote:Hi, ive run into a problem designing a database (php and mysql) for submitting student projects. ive basically created 4 tables:
1) holds the project details eg. project title, project outline etc
students can pick from these projects
2) holds the project owners, eg when a project is picked its owner is entered into this table showing that the project has been picked and which student has picked the project and has a foreign key pointing to project details
3) holds the project files which the student will submit for a project and has a foreign key pointing to project details
4) holds the project grades which a lecturer will assign to a student for a
project and has a foreign key pointing to project details

ive decided to create a class for each table ( i assume this is the correct way of doing it???) . for example the class project will have methods getProjectName(), createProject() etc. while the class projectgrades will have methods deleteFile(), viewFile() etc.
now due to the hierarchial design of my tables ive decided to implement the classes in a similar fashion
as a result of the relationship between the classes i thought it best to use inheritance eg. the projectowner class inherits the projectDetails class and likewise with the projectgrades class and projectfiles class.
now i have also created a database operations class that performs all of the mysql querys eg, inserting, updating removing table data. but the problem arises when i try to implement this dataopertions class. what id like to know is how exactly do i implement this class in each of the classes ive named above in order to manipulate the database tables??? im failrly sure i dont use inheritance because the database opertions class doenst model a real world object. all i want to know is how does the class for each table make use of the services of the database operations class, what would be the best way of doing this???
any help on this would be much appreciated. its driving me mad!! :x
I don't really think this is an ideal way to deal with data to be honest with you. To do this kind of thing, you should use an ObectDBMS or ObejctRelationalDBMS.

That said, hopefully this will help you with the OO part of the question here.

Use a singleton object. Furthermore, each object should have a reference to this object. Internally, your objects would make calls to this data operations class as such:

Code: Select all

$this->operationsObj->someOperationMethod();
You can use a factory object or function to take care of this for you. As it turns out each object, it will assign a reference to your operations object. Another option (tho not as tidy) is to create your operations object first then have each create it's reference to it during instantiation (via the constructor). This isn't anywhere near as ideal as the name of the instantiated operations object becomes critical.

Cheers,
BDKR