Hi,

I am trying to persist Moose-objects via DBIx::Class in an Oracle database.

This is the first time ever that I use DBIx::Class so any hints would be very welcome...

As a simple example my class has 3 attributes, "id" as integer (which will be used as primary key in the table) and two string-attributes "a" and "b".

Here is the table I want my objects to be persisted in:

create table hubba (id int, a varchar2(20), b varchar2(20));
The I create a Schema-class like this:
package SMU::Schema; use base qw(DBIx::Class::Schema); __PACKAGE__->load_namespaces; 1;
Then I implement my class like this:
package SMU::Schema::Result::Hubba; use DBIx::Class; use Moose; extends 'DBIx::Class'; has 'id' => ( is => "ro", isa => "Int"); has 'a' => ( is => "rw", isa => "Str"); has 'b' => ( is => "rw", isa => "Str"); sub dump { my($this)=@_; return $this->id . " - " . $this->a . " - " . $this->b; } __PACKAGE__->load_components('Core'); __PACKAGE__->table('hubba'); __PACKAGE__->add_columns( id => { data_type => 'integer', size => 16 }, a => { data_type => 'varchar', size => 20 }, b => { data_type => 'varchar', size => 20 }, ); __PACKAGE__->set_primary_key('id'); 1;
So now I can save/retrive data like this:
my $schema = SMU::Schema->connect("dbi:Oracle:SMU", "user", "pw") or d +ie $!; # find row with id=1 and return as object my $h1 = $schema->resultset('Hubba')->find(1); # persist new object $schema->resultset('Hubba')->create( { id => 2, a => "blah", b => "blubb", });
While the above works, there are a lot of things that I don't like about it very much.

For a start Moose generates a "new"-constructor while DBIx::Class uses a "create"-constructor. While I can use the DBIx::Class constructor to get back an instance of my Moose-class, for whatever reason I don't seem to be able to use the Moose-constructor any more (calling e.g. SMU::Schema::Result::Hubba->new(id => 1) results in an error).

But ok I could live with this.

What I dislike the most is that in my class-implementation I basically have to tell Moose about the attributes (via "has") as well as DBIx::Class (via add_columns). Now you may argue that this is not quite the same thing (and I would agree) but I wonder if there is some MooseX::?? extension that would allow me to map my attributes to columns via some trait or the like ... the above just looks very clumsy to me.

I would be very grateful for anyone that could show me a better way of persisting a simple Moose-object in a database via DBIx::Class.

Many thanks!


In reply to DBIx::Class and Moose by morgon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.