roman has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

modeling a DBIC classes hierarchy I went into a problem which must have been solved thousand times.

I have a base class, which instances are straightforwardly serialized into a table (object attribute is a table column) via DBIx::Class.

Then I have an extension class, with additional attributes also to be serialized into database.

I (intuitively) created a new table containing only the extending attributes and foreign key referencing the base table.

So for one logical object I have two physical rows in a database. Is there a way how to reasonably represent such subclass using DBIx::Class, i.e. to have DBIC instance working (insert, update, search) with two (more) records at once?

I would like to avoid a "pure database" solution based on updateable database view.

I can use "inline" view as the table. Of course they are not updatable.

select * from ( select ... from table1, table2 where ... ) where ...

I appreciate any clue and hope for simple answer like: "... well known problem with many good solutions, just see the discussion at ...".

Replies are listed 'Best First'.
Re: Extension of DBIC "model" class
by CountZero (Bishop) on May 09, 2008 at 16:03 UTC
    Have a look at DBIx::Class::Manual::Joining

    Your problem is easily solved by joining these tables together.

    DBIC has ample methods for it.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      No, this is different case, all examples in joining manuals describe how to access related but independent objects - there is always one object one row.

      What I am looking for is how to implement the inheritance with additional attributes in DBIC.

      An unsophisticated example:

      -- I have an animal table describing species create table myschema.animal ( species varchar2(64) primary key, region varchar2(64) ); -- and some tables extending the animal create table myschema.animal_mammal ( species varchar2(64), -- foreign key max_height number, quadrupedal number(1) default 1 -- 0 or 1 ); create table myschema.animal_bird ( species varchar2(64), -- foreign key max_wingspan number, number_of_eggs number );

      I would like to create the DBIC classes for mammal and bird so I can work with them as single objects.

      my $dog = $schema->resultset('Mammal')->find('Canis familiaris'); warn $dog->region, ' ', $dog->max_height; my $chimp = $schema->resultset('Mammal')->create({ 'species' => 'Pan troglodytes', 'region' => 'Africa', 'quadrupedal' => 0, });