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

[prev] [thread] [next] [Date index for 2005/10/24]

From: Steven Mackenzie
Subject: [CDBI] Making Class::DBI objects from a search_ method result?
Date: 14:13 on 24 Oct 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

_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

[CDBI] Making Class::DBI objects from a search_ method result?
Steven Mackenzie 14:13 on 24 Oct 2005

Generated at 14:18 on 27 Oct 2005 by mariachi v0.52