[Dbix-class] inheritance

Matt S Trout dbix-class at trout.me.uk
Wed Sep 13 14:23:13 CEST 2006


Hermida, Leandro wrote:
> Hello,
>  
> I have a question about inheritance (or generalization/specialization 
> hierarchies?) when building your classes using DBIx::Class.  If I have a 
> data model with inheritance:
>  
> -- abstract class
> create table parameterizable (
>   id                INT NOT NULL
>   ... some basic columns here...
> );
>  
> -- abstract class
> create table protocol (
>   id                INT NOT NULL,
>   name              VARCHAR(200)
> );
>  
>  
> create table growth_protocol (
>   id                INT NOT NULL,
>   medium            VARCHAR(200),
>   temperature       VARCHAR(200)
> );
>  
> create table treatment_protocol (
>   id                INT NOT NULL,
>   compound          VARCHAR(200),
>   delivery_method   VARCHAR(200)        
> );
>  
>  
> alter table parameterizable
>   ADD CONSTRAINT pk_id_01 PRIMARY KEY (id)
> ;
>  
> alter table protocol
>   ADD CONSTRAINT pk_id_02 PRIMARY KEY (id)
>   ADD CONSTRAINT fk_id_02 FOREIGN KEY (id) REFERENCES parameterizable (id)
> ;
>  
> alter table growth_protocol
>   ADD CONSTRAINT pk_id_03 PRIMARY KEY (id)
>   ADD CONSTRAINT fk_id_03 FOREIGN KEY (id) REFERENCES protocol (id)
> ;
>  
> alter table treatment_protocol
>   ADD CONSTRAINT pk_id_04 PRIMARY KEY (id)
>   ADD CONSTRAINT fk_id_04 FOREIGN KEY (id) REFERENCES protocol (id)
> ;
>  
> How does one represent this using DBIx::Class?  Since abstract classes 
> like Parameterizable and Protocol should never get instantiated directly 
> then how do I create DBIx::Class packages for GrowthProtocol and 
> TreatmentProtocol which will transparently join with the to the abstract 
> class tables and get the accessors when instantiating objects?

Create classes for them anyway, then have something like (in TreatmentProtocol)

__PACKAGE__->might_have('_protocol', 'My::Schema::Protocol', undef, { proxy => 
[ qw/compound .../ ] });

then you can do $treatment_protocol->compound and it'll Just Work (though 
you'll need to specify the prefetch for _protocol to make it a single query).

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list