[Catalyst] Model objects from multiple DBIx::Class rows

Lukas Thiemeier spamcatcher at thiemeier.net
Thu Nov 22 16:33:48 GMT 2012


Hi Stephen,

As far as I know, creating result class without underlying table is not
supported.

Having a class without table, but with relationships doesn't realy make
sense to me. A relationship always has two sides.

I am not sure if I really understand your problem. You have a "user"
table and a related "picture" table? My approach would be to select the
user from the user-table, and use "prefetch" to populate the
pictures-relation with the same call. You will get a $user object with
the "pictures" relationship already populated.

https://metacpan.org/module/DBIx::Class::ResultSet#prefetch

If you want to update several related tables with a single method call,
take a look at RecursiveUpdate. It is not the fastest solution, and
there might be situations where it is not suitable, but it works fine in
most cases. And it allows you to pass nested hash- and arrayrefs to
recursive_update, and therefore updating or creating several related
rows with very little code. For example, HTML::FormHandler uses R-U to
update the database.

https://metacpan.org/module/DBIx::Class::ResultSet::RecursiveUpdate

It is also possible to create several result classes for the same table,
if you need different behavior in different situations. You cound, for
example, create a "Montage" class, which works on the "user" table, and
add custom code which is only needed by montages. The old "user" class
will still be available.

cheers, Lukas



On 11/22/2012 04:59 PM, Stephen Shorrock wrote:
> Hi,
> 
> I was wondering whether there is any advice out there about how to use
> and manage objects within a Catalyst application where the objects
> contain DBIx::Class schema model objects that are also within the
> application.
> 
> I've considered creating simple moose objects that get passed the
> schema objects and use rows returned from the schema objects to
> populate 'local' values but this doesn't seem right. The trouble I
> have is that data for these objects is stored in the database but
> their i sno single table for the object them selves for example I
> might have a Montage object that has a $User and several @Pictures the
> User record is stored in the database and the Picture records are also
> stored as individual rows in a pictures table. I want to manipulate
> the users along with their pictures as an object them selves, for
> example given a User DBIx::class objects create a method (eg
> get_montage) that returns a Montage.
> 
> Is there a simple solution perhaps creating a DBIx::class object that
> doesn't actually have an underlying table but can still utilize
> relationships and have User::get_montage return an instance of one of
> these?  Or am I overlooking something else that is obvious?
> 
> Thanks in advance
> 
> Stephen
> 
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/




More information about the Catalyst mailing list