Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Associating Moose class with database table

by nysus (Priest)
on Jun 13, 2018 at 00:13 UTC ( #1216514=perlquestion: print w/replies, xml ) Need Help??
nysus has asked for the wisdom of the Perl Monks concerning the following question:

Can someone please step me through the basics of creating a Moose class and automatically associating attributes in the class with columns in a database? For example, it would be cool if I could add an attribute to a class and have it automagically generate a mysql column based on the properties of the attribute. Surely there must be a module that makes this easy. I've kind of started doing this on my own but it would be stupid to reinvent the wheel. I don't know much about "orm" or other acronyms so the simplest terms you can put it in, the better. Thanks so much!

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

  • Comment on Associating Moose class with database table

Replies are listed 'Best First'.
Re: Associating Moose class with database table
by 1nickt (Monsignor) on Jun 13, 2018 at 01:45 UTC

    See DBIx::Class which is the most widely used Perl ORM. The docs are comprehensive, but you will want to start with a tutorial or test DB, not launch into your real app. Depending on your preference you start with your DB and make classes, or start with classes and make your DB. The latter approach would allow you to do what you want.

    Personally I am comfortable writing SQL so I choose the former, generating my classes. DBIx allows you to modify the generated schema classes, eg by using Moose or adding custom functions, and preserves your changes upon regeneration after you alter the DB.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Associating Moose class with database table
by trwww (Priest) on Jun 13, 2018 at 05:19 UTC
Re: Associating Moose class with database table
by Your Mother (Bishop) on Jun 13, 2018 at 15:54 UTC

    There is a bit of an implication here the DBIx::Class is Moose based. It's not. It can work with it if tooled carefully. I don't recommend it; or Moose at this point. In my view, it's too big, too slow, and does too much for its average/median use case.

      In my view, (Moose is) too big, too slow, and does too much for its average/median use case.

      I agree, and use Moo by preference wherever possible, including in some DBIx::Class result classes. However, the OP is all-in with Moose, I believe.

      (Some people would make the same statement about DBIx::Class. There is a lot of careful tooling required to effectively use it even without adding the Moose layer. Most applications don't warrant the extended effort required to make it work well, so using it in those applications can easily become an exercise in frustration. For larger frameworks it becomes indispensable, as does Moose in certain cases of its own.)


      The way forward always starts with a minimal test.
Re: Associating Moose class with database table
by Anonymous Monk on Jun 15, 2018 at 18:43 UTC
    DML changes adding a column with the proper attributes automagically is not something that is very commonly found.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1216514]
Approved by taint
Front-paged by haukex
help
Chatterbox?
NodeReaper settles into the armchair by the fire. You weren't using it were you?

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2018-07-22 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (454 votes). Check out past polls.

    Notices?