I'm writing an object oriented forum, and I've got a topic class and a post class, corresponding to a topic table and a post table in my MySQL database.
I'm trying to decide between two alternatives to construct the post objects in my PHP app. They are:
-Have the topic object do one little query for post IDs (SELECT postid FROM posttable WHERE topicid=xx), and call the post constructor for each ID. Then the post constructor queries the database once for each post (SELECT * FROM posttable WHERE postid=xx).
-Have the topic object do one big query, (SELECT * FROM posttable WHERE topicid=xx), call the post constructor without an ID to create a blank object, and have a setProperties() method of the post object, called by the topic object after the constructor to give the post the rest of its data.
The first way of doing things seems much more elegant and simple, but it seems like the latter would be much more efficient. But I don't really know. I face a similar problem to this with some other object hierarchies on my site, so any thoughts on optimization will be very much appreciated!