Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.61 KB | None | 0 0
  1. Why does $product->brand_supplier return null?
  2.  
  3.  
  4. ## Query and result
  5. select p.product_id, bs.brand_supplier_id, b.brand_id, b.name
  6. from product as p
  7. left join brand_supplier as bs
  8.     on bs.brand_supplier_id=p.brand_supplier_id
  9. left join brand as b
  10.     on b.brand_id=bs.brand_id
  11. where p.product_id=7797923;
  12. +------------+-------------------+----------+--------+
  13. | product_id | brand_supplier_id | brand_id | name   |
  14. +------------+-------------------+----------+--------+
  15. |    7797923 |               492 |        1 | adidas |
  16. +------------+-------------------+----------+--------+
  17.  
  18. ## Product.pm
  19. package Mine::DBIx::Schema::Result::Product;
  20. use base qw/DBIx::Class::Core/;
  21.  
  22. __PACKAGE__->table('product');
  23. __PACKAGE__->add_columns(qw/ name product_id description fraud_percent /);
  24. __PACKAGE__->set_primary_key('product_id');
  25. __PACKAGE__->has_one(brand_supplier => 'Mine::DBIx::Schema::Result::BrandSupplier', brand_supplier_id);
  26. __PACKAGE__->has_many(styles => 'Mine::DBIx::Schema::Result::Style', product_id);
  27. 1;
  28.  
  29.  
  30. ## Brand.pm
  31. package Mine::DBIx::Schema::Result::Brand;
  32. use base qw/DBIx::Class::Core/;
  33. __PACKAGE__->table('brand');
  34. __PACKAGE__->add_columns(qw/ name is_kids is_couture is_handbag brand_id /);
  35. __PACKAGE__->set_primary_key('brand_id');
  36. __PACKAGE__->belongs_to(brand_supplier => 'Mine::DBIx::Schema::Result::BrandSupplier', 'brand_id');
  37. 1;
  38.  
  39. ## BrandSupplier.pm
  40. package Mine::DBIx::Schema::Result::BrandSupplier;
  41. use base qw/DBIx::Class::Core/;
  42.  
  43. __PACKAGE__->table('brand_supplier');
  44. __PACKAGE__->add_columns(qw/ brand_id brand_supplier_id/);
  45. __PACKAGE__->set_primary_key('brand_supplier_id');
  46. __PACKAGE__->has_one(brand => 'Mine::DBIx::Schema::Result::Brand', 'brand_id');
  47. __PACKAGE__->belongs_to(product => 'Mine::DBIx::Schema::Result::Product', 'brand_supplier_id');
  48.  
  49. 1;
  50.  
  51. ### Script:
  52. #!/usr/bin/perl -w
  53.  
  54. use strict;
  55. use Mine::DBIx::Schema;
  56. use Data::Dumper;
  57.  
  58. my $dsn = 'DBI:mysql:database=product;host=localhost';
  59. my $user = 'user';
  60. my $pass = 'pass';
  61. my $schema = Mine::DBIx::Schema->connect($dsn, $user, $pass);
  62.  
  63. my $product = get_product( 7797923 );
  64.  
  65. print "Product name: " . $product->name . " and product id is " . $product->product_id ."\n";
  66. print Dumper($product->brand_supplier);
  67.  
  68. sub get_product {
  69.     my $product_id = shift;
  70.     return $schema->resultset('Product')->search( { product_id => $product_id } )->single;
  71. }
  72.  
  73. ## This returns this output:
  74. 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.
  75. Product name: Barracks Premier Wide and product id is 7797923
  76. $VAR1 = undef;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement