Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^2: Object Persistence, Moose and playing nicely with ActiveRecord

by pdcawley (Hermit)
on Sep 08, 2008 at 14:59 UTC ( #709787=note: print w/replies, xml ) Need Help??

in reply to Re: Object Persistence, Moose and playing nicely with ActiveRecord
in thread Object Persistence, Moose and playing nicely with ActiveRecord

Single table inheritance. You have a class, Document, stored in the documents (I know) table you have:
package Review use Moose extends 'Document'
The documents table has a set of columns which is the superset of attributes of Document and all its subclass and a type column which contains the name of the class for that row. Document->find finds Documents, Reviews and any other subclasses of document, Review->find finds only reviews (and any subclasses).

Composed of: A way of getting round primitive obsession. You can declare something like:

composed_of :price, :class_name => 'Money', :mapping => %w{cents currency}
And ActiveRecord handles turning cents and currency columns into a Money object when the object is pulled out of the database breaking the price into cents and currency the the object's put back in there.

Bells and whistles: Mostly to do with declaring relationships between classes with has_many/belongs_to etc. If you want me to regurgitate the ActiveRecord::Base documentation then I will, but it's far from being the world's best documentation in the first place and my paraphrasing skills are poor. Most of the perly ORMs appear to have sensible equivalents already so it won't be beyond my wit to sort out the mapping.

Replies are listed 'Best First'.
Re^3: Object Persistence, Moose and playing nicely with ActiveRecord
by perrin (Chancellor) on Sep 08, 2008 at 16:41 UTC
    There are inheritance modules for Class::DBI and Tangram, but I haven't seen any for Rose::DB::Object or DBIx::Class. It's a relatively easy thing to build though, just making classes for each type. The only tricky part is turning Documents into Reports, which I've implemented in my own Class::DBI stuff before by having the base class look at a type column and then instantiate an object of the right class with the current primary key.

    The rest of the stuff is standard issue ORM functionality that all the Perl ones have.

Re^3: Object Persistence, Moose and playing nicely with ActiveRecord
by Corion (Pope) on Sep 08, 2008 at 15:09 UTC

    I'm not aware of any Perl-ORM implementing Single Table Inheritance (or rather, Single Table Object Storage). I guess you can fake this by creating updateable views for all classes if your underlying DB supports that.

    For composition, I think DBIx::Class (like Class::DBI) has inflation/deflation links which can be used to instantiate the appropriate objects for fields. But I haven't used either.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://709787]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-03-30 13:45 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (360 votes). Check out past polls.