An OOP Design Problem

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Zerox Millienium
Forum Newbie
Posts: 3
Joined: Thu Jul 15, 2004 12:31 pm

An OOP Design Problem

Post by Zerox Millienium »

Greetings to all,

I have a question regarding an OOP design problem. Say I have come up with an OO design of classes for instance
Session, User and UserPreferences

A Session object has a User object which has a UserPreferences object.

I have 3 tables in a database a Session, User and UserPreference, and that the classes I have designed maps directly to these tables.

Somehow I got stuck with how am I supposed to fill the data into the classes from the tables respectively?
Do I use an inner join statement and populate the object properties? Or do I fetch progressively accordingly to the order of containment, since the Session object is the first container, I fetch the data for the session first, followed by the User and then the UserPreferences.

Of course, common sense tells me that the inner join might be better off since it's only a single request to the database rather than 3 sql statements one after the other till fill that containment hierarchy of objects.

But seriously, is there a better or more appropriate approach to mapping OO software designs to a RDBMS tables. What I intend to do is to map classes of my application to data rows of the data tables in a database.

Thanks.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: An OOP Design Problem

Post by timvw »

It depends on the code. If you know beforehand that you'll need all the references it's more efficient to eager load all the data. On the other hand, if you're not going to use all the references it's more efficient to lazy load the items because otherwise you would be fetching that you're not going to use (the disadvantage is that this can lead to n+1 queries).

Most tools allow the user to explicitely mention exactly which strategy they want (eager or lazy loading) use, so i would recommend that you too support both options.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: An OOP Design Problem

Post by Christopher »

Zerox Millienium wrote:Of course, common sense tells me that the inner join might be better off since it's only a single request to the database rather than 3 sql statements one after the other till fill that containment hierarchy of objects.
The join is a loading optimization, but should not affect the design really. You could even move that loader to a separate class, much like a Data Mapper. As long as the classes have clear initialization interfaces, then it does not matter which direction you go -- and you should be able to change directions later if you need to. I would try to separate the use cases from the loading to make things flexible.
(#10850)
Post Reply