Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Why does $product->brand_supplier return null?
- ## Query and result
- select p.product_id, bs.brand_supplier_id, b.brand_id, b.name
- from product as p
- left join brand_supplier as bs
- on bs.brand_supplier_id=p.brand_supplier_id
- left join brand as b
- on b.brand_id=bs.brand_id
- where p.product_id=7797923;
- +------------+-------------------+----------+--------+
- | product_id | brand_supplier_id | brand_id | name |
- +------------+-------------------+----------+--------+
- | 7797923 | 492 | 1 | adidas |
- +------------+-------------------+----------+--------+
- ## Product.pm
- package Mine::DBIx::Schema::Result::Product;
- use base qw/DBIx::Class::Core/;
- __PACKAGE__->table('product');
- __PACKAGE__->add_columns(qw/ name product_id description fraud_percent /);
- __PACKAGE__->set_primary_key('product_id');
- __PACKAGE__->has_one(brand_supplier => 'Mine::DBIx::Schema::Result::BrandSupplier', brand_supplier_id);
- __PACKAGE__->has_many(styles => 'Mine::DBIx::Schema::Result::Style', product_id);
- 1;
- ## Brand.pm
- package Mine::DBIx::Schema::Result::Brand;
- use base qw/DBIx::Class::Core/;
- __PACKAGE__->table('brand');
- __PACKAGE__->add_columns(qw/ name is_kids is_couture is_handbag brand_id /);
- __PACKAGE__->set_primary_key('brand_id');
- __PACKAGE__->belongs_to(brand_supplier => 'Mine::DBIx::Schema::Result::BrandSupplier', 'brand_id');
- 1;
- ## BrandSupplier.pm
- package Mine::DBIx::Schema::Result::BrandSupplier;
- use base qw/DBIx::Class::Core/;
- __PACKAGE__->table('brand_supplier');
- __PACKAGE__->add_columns(qw/ brand_id brand_supplier_id/);
- __PACKAGE__->set_primary_key('brand_supplier_id');
- __PACKAGE__->has_one(brand => 'Mine::DBIx::Schema::Result::Brand', 'brand_id');
- __PACKAGE__->belongs_to(product => 'Mine::DBIx::Schema::Result::Product', 'brand_supplier_id');
- 1;
- ### Script:
- #!/usr/bin/perl -w
- use strict;
- use Mine::DBIx::Schema;
- use Data::Dumper;
- my $dsn = 'DBI:mysql:database=product;host=localhost';
- my $user = 'user';
- my $pass = 'pass';
- my $schema = Mine::DBIx::Schema->connect($dsn, $user, $pass);
- my $product = get_product( 7797923 );
- print "Product name: " . $product->name . " and product id is " . $product->product_id ."\n";
- print Dumper($product->brand_supplier);
- sub get_product {
- my $product_id = shift;
- return $schema->resultset('Product')->search( { product_id => $product_id } )->single;
- }
- ## This returns this output:
- Use of uninitialized value in scalar assignment at /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/List/MoreUtils.pm line 24.
- Product name: Barracks Premier Wide and product id is 7797923
- $VAR1 = undef;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement