Subject: | Search with has_many relation fails at using ORDER BY RAND() |
Hi, first i apologies about my english, i will try to express my self
the best i can. I found this issue that i detail next. Thanks
DBIx-Class: DBIx-Class-0.08119
Perl Version: 5.8
OS: Ubuntu 8.10
package WEBEXPERTO::Modulos::Catalogue::Model::Product;
use strict;
use warnings;
use base qw/DBIx::Class/;
use base qw/WEBEXPERTO::Core::MEB::Model::Model/;
__PACKAGE__->load_components(qw/PK::Auto ResultSetManager Core/);
__PACKAGE__->table('catalogue_products');
__PACKAGE__->add_columns(qw/id_product id_node name_product
description_product is_actived_product datetime_added_product
datetime_last_modified_product date_start_publish_product
date_finish_publish_product imagen_product resumen_product
codigo_product destacado_product oferta_product skin_name/);
__PACKAGE__->set_primary_key(qw/ id_product /);
__PACKAGE__->has_many(
'productPrice' =>
'WEBEXPERTO::Modulos::Catalogue::Model::ProductPrice',
{'foreign.idProduct' => 'self.id_product'},
{cascade_delete => 0},
);
__PACKAGE__->has_one(
'node' => 'WEBEXPERTO::Modulos::Catalogue::Model::Node',
{'foreign.id' => 'self.id_node'},
{cascade_delete => 0},
);
1;
package WEBEXPERTO::Modulos::Catalogue::Model::ProductPrice;
use strict;
use warnings;
use base qw/DBIx::Class/;
__PACKAGE__->load_components(qw/PK::Auto Core/);
__PACKAGE__->table('catalogue_productPrices');
__PACKAGE__->add_columns(qw/id idProduct idPriceList idCurrency value/);
__PACKAGE__->set_primary_key(qw/ id /);
__PACKAGE__->has_one(
'currency' =>'WEBEXPERTO::Modulos::Catalogue::Model::Currency',
{'foreign.id' => 'self.idCurrency' },
);
__PACKAGE__->belongs_to(
'priceList' =>'WEBEXPERTO::Modulos::Catalogue::Model::PriceList',
{'foreign.id' => 'self.idPriceList' },
);
1;
#CODE
my @productPrefetch;
push(@productPrefetch,'node');
push(@productPrefetch,{'productPrice'=>'currency'});#has_many relation
my $searchAttributes = {
prefetch => \@productPrefetch,
rows => 3,
order_by=> 'RAND()', #<-- cause the bug
group_by => 'me.id_product',
};
my $rsProducts = $schema->resultset('Product')->search({},
$searchAttributes);
my $totalItems = $rsProducts->pager()->total_entries;
What i expect is a set of products with has_many relation (productPrice)
loaded with all prices. What i get is three products but eachone repeted
3 times,
seems that group_by is not working.
Sql query looks like this:
SELECT me.id_product, me.id_node, me.name_product,
me.description_product, me.is_actived_product,
me.datetime_added_product, me.datetime_last_modified_product,
me.date_start_publish_product, me.date_finish_publish_product,
me.imagen_product, me.resumen_product, me.codigo_product,
me.destacado_product, me.oferta_product, me.skin_name, productPrice.id,
productPrice.idProduct, productPrice.idPriceList,
productPrice.idCurrency, productPrice.value, currency.id, currency.name,
currency.symbol FROM
(SELECT me.id_product, me.id_node, me.name_product,
me.description_product, me.is_actived_product,
me.datetime_added_product, me.datetime_last_modified_product,
me.date_start_publish_product, me.date_finish_publish_product,
me.imagen_product, me.resumen_product, me.codigo_product,
me.destacado_product, me.oferta_product, me.skin_name, me.stock_product,
me.iva_product, me.marca_product FROM catalogue_products me GROUP BY
me.id_product ORDER BY RAND() LIMIT 3) me
LEFT JOIN catalogue_productPrices productPrice ON productPrice.idProduct
= me.id_product LEFT JOIN catalogue_currencies currency ON currency.id =
productPrice.idCurrency ORDER BY RAND(), productPrice.idProduct;
The last 'ORDER BY RAND()' mix up the product results (its seems like
group_by is ignored). If you dont specify order_by attribute (with
'RAND()') its works just fine.
If you need more details please let me know. Thanks again.