[CDBI] CDBI "collections" and Class::DBI::Iterator

Tom Metro tmetro+cdbi at gmail.com
Sun Aug 27 20:03:02 BST 2006


I had a need to write some methods that operated on collections of CDBI 
records, and wondered what was the preferred approach and in what module 
those methods should live.

In the existing code I was working on there was a parent record type and 
a child record type with a parent->has_many(child) type relationship set 
up. The methods for dealing with bunches of child records currently 
lived in the parent class, which seemed out of place.

I researched this briefly and it led me to Class::DBI::Iterator. 
Subclassing it, as suggested in the CDBI man page, seemed like the 
obvious way to go for adding functionality that should operate on 
collections. However looking over the CDBI wiki, mailing list archives, 
and general Google searches turned up no prior art of it being used that 
way.

This must be a fairly common need, so I'm surprised there isn't more 
discussion of it, but maybe I've missed it. The state of 
Class::DBI::Iterator also made me wonder whether it whether it was 
getting much use in this way. (It seemed rather bare-bones. I needed to 
add a few rather basic additional methods in my subclass to make it 
work. I'll cover this in a separate posting.)

I'd like to hear opinions on whether this is the recommended approach, 
and if not, what is?

A minor related issue is where to put the Class::DBI::Iterator subclass. 
It's so closely related to the model class that ideally it should live 
in the same file, which is how I implemented it.

  -Tom

-- 
Tom Metro
Venture Logic, Newton, MA, USA
"Enterprise solutions through open source."
Professional Profile: http://tmetro.venturelogic.com/



More information about the ClassDBI mailing list