I am currently trying to implement the following script with DBIx:
The product table contains “common products” and “products with a package” (package products are collections of common products):
package Product; use base 'DBIx::Class::Core'; __PACKAGE__->table("products"); __PACKAGE__->add_columns( "productId", { data_type => "varchar", is_nullable => 0, size => 10}, "name", { data_type => "varchar", is_nullable => 1, size => 150}, "type", { data_type => "enum", default_value => "general", extra => { list => ["general", "bundle"], }, is_nullable => 0, });
As you can see, the product is a generic product or the product package is saved in the type column.
Now I would like to encapsulate this information in the class identifier: I would like to have the following classes:
- Product (
type doesn't matter) - BundleProduct (
type = 'bundle') - GeneralProduct (
type = 'general')
I wrote:
package BundleProduct; use base 'Product'; __PACKAGE__->resultset_attributes({ where => { 'type' => 'bundle' } }); 1;
and
package GeneralProduct; use base 'Product'; __PACKAGE__->resultset_attributes({ where => { 'type' => 'general' } }); 1;
But when doing
my @allProducts = $schema->resultset('BundleProduct')->all;
All common products are displayed. Although the resulting objects have an instance of BundleProduct , the generated SQL contains the WHERE clause of the GeneralProduct class ( type = 'general'). Even worse: if I try to get Product (the base class of BundleProduct and GeneralProduct ), the condition type = 'general' will also be applied! It seems that the definition inside GeneralProduct overwrites all other definitions.
What is wrong with my design?
Victor-Philipp Negoescu
source share