Reputation: 997
I assumed this was good practice, for reasons such as it helps to clearly represent relationships between classes. However, according to this website it's not:
http://www.tonymarston.net/php-mysql/good-bad-oop.html#a5
So, is having a class for each database table good OO practice or not?
Upvotes: -1
Views: 1584
Reputation: 57
I disagree with your statement that a separate class for each database table may not be a good idea for a junction table. In the case where you have two tables T1 and T2 which exist in a many-to-many relationship you would deal with this by creating a juntion (or intersection) table J so that you could create two one-to-many relationships. Table J would have foreign keys which link it to T1 and T2 and a primary key which combines both those foreign keys.
While I have data in each table class which identifies the existence of relationships with other tables I never have any code inside any table class to deal with any relationship. Instead I build separate user transactions using specific Controllers to deal with each type of relationship. I would have separate sets of user transactions to maintain the contents of tables T1 and T2, ignoring any relationships, then I would create separate transactions, using a specific Controller, to handle each of the one-to-many relationships between T1->J and T2->J.
In my framework all code for dealing with relationships is either in a Controller or another framework component, never within a table class.
Upvotes: 0
Reputation: 9933
Briefly reading the article in the introduction these points are said to 'complaints' against another article the author had written. I believe the author is actually saying that a class per table is a good approach and explains why, defending it against remarks such as "This code leads to a problematic dependency between the DB and the class".
Having a table per class is certainly a good place to start from, however like any pattern it is not necessarily going to be something that'll be required in all situations. An example being a junction table where perhaps just one class can handle interactions for numerous different junction tables. Likewise you may have tables with a one to one relationship that fits the program better as a single class.
Upvotes: 1