[CDBI] Making Class::DBI objects from a search_ method result?

Steven Mackenzie steven.mackenzie at aptile.co.uk
Mon Oct 24 15:13:43 BST 2005


Hello,

I would like to add a convenience method to once of my Class::DBI
objects, so that I can get a collection of related CDBI instance objects
via a JOIN, rather than by following has_many relations.

>From looking at the doc
http://search.cpan.org/dist/Class-DBI/lib/Class/DBI.pm#has_a
and
http://cdbi.dcmanaged.com/wiki/Beginners_guide#Entity_Relationship_Diagram_.28ERD.29
I think something like this should be possible:

 # add a sub to get all persons in the band that created a CD; assumes
 # that the current CD was created by a band!
 CD->set_sql
  ( persons =>
    qq{
        SELECT p.*
        FROM CD cd
          JOIN artist a on cd.artistid = a.artistid
          JOIN artistgroup ag on a.artistid = ag.bandartistid
          JOIN person p on ag.personartistid = p.artistid
        WHERE cd = $self.cdid
      }
  );

  # ... elsewhere ...
  my @persons = $cd->search_persons();

The array @persons is now an array of hashes.  I'd rather it was an
array of PERSON rows though. Is there a nice way to do that?

Steven




More information about the ClassDBI mailing list