Combining Collection & GenericObjectCollection?
Hi all,
In Professional PHP5, Chapters 5 & 9, there are discussions on how to implement collections of a base object. For the most part, these seem redundant to me, and the GenericObjectCollection class in Chapter 9 is the same as the Collection class, except it's DB-aware. I'm trying to think through a way to efficiently combining their functionality. Possible idea is below. DBObject is my renamed GenericObject, and pointless code is removed.
class Group extends DBObject
{
private $_users;
...
public function __construct()
{
...
$this->_users = new Collection();
$this->_users->setLoadCallback('_loadUsers', $this);
}
...
public function _loadUsers(Collection $col)
{
// this is where, I guess, I would put some condensed implementation of GenericObjectCollection's loading logic?
}
}
As you can see, I'm guessing that Collection's lazy-instantiation callback, _loadUsers, is where I would do some DB ops, like is described in GenericObjectCollection, to get all matching User objects from the User table in one query (using "... WHERE id IN (list-of-userids)"). But I guess I'm getting hung up on just how much screwing I can do with this. Maybe my head's just fuzzy from trying to code on a Saturday night.
Ideas on this? Thanks!
--Scott
|