[Dbix-class] Encapsulate multiple steps for insertion
Jess Robinson
castaway at desert-island.demon.co.uk
Wed Jan 17 09:17:58 GMT 2007
On Thu, 11 Jan 2007, Tobias wrote:
> Hi everybody,
>
> I'm writing a forum application with DBIx::Class and Catalyst. Every post in a
> forum is stored using two tables (for performance reasons):
>
> * posts - (DBIC class "Forum::Post")
> contains basic data for every post
> has_one relationship with Forum::PostText
>
> * post_texts - (DBIC class "Forum::PostText")
> contains just the texts
> belongs_to Forum::Post
>
> I'm wondering what's the best way to encapsulate this because I don't want to
> make calls to several DBIx::Class classes everywhere I want to insert a new
> post (i.e. in Catalyst controllers). Should I just create a third class outside
> the Schema namespace which utilizes all neccessary DBIx::Class classes for
> inserting a new post to hide the underlying separation into two tables from
> the rest of the application? What I want is to call _ONE_ method in my
> Catalyst controllers to insert a new post into the database. The controller
> shouldn't worry if the data gets stored in one, two or ten tables.
>
> Maybe there's some sort of DBIx::Class magic to do this without having to
> add an extra layer myself?
>
Hmm, seems I missed this conversation. I'm in the process of adding this
to a DBIx::Class branch (bulk_create).
The idea will essentially be that you call create once with enough info to
create all the needed related objects, and it creates them in a
transaction (all or nothing).
Syntax is currently fairly simple:
$item->create({Name => 'fred',
Parent => { 'Name' => 'parentoffred'},
Tags => [ { 'Tag' => 'foo'}, { 'Tag' => 'bar' }],
});
.. etc, where each hashref can be either a hashref of the related tables
cols/vals, or an actual object of that table. Use a single hashref for a
one-to-one rel, and an arrayref for one-many rels. The keys there are the
relnames or the normal column names, depending on the rel types.
Jess
More information about the Dbix-class
mailing list