DBIx :: A collection of integer sets of integers with the same table in prefetch
I am trying to implement finite element chaining in my application.
Here are some methods from my resultset class:
package Schema::ResultSet::Category;
use base 'DBIx::Class::ResultSet';
sub with_translation {
my ($self, $lang) = @_;
$self->search(
{
'language.code' => $lang,
},
{
prefetch => {
'category_i18ns' => 'language'
}
}
);
}
sub with_products {
my ($self, $lang) = @_;
$self->search(
{
'language.code' => $lang,
},
{
prefetch => {
'products' => {
'product_i18ns' => 'language',
},
},
},
);
}
sub with_categories {
my ($self, $lang) = @_;
$self->search(
{
'language.code' => $lang,
},
{
prefetch => {
'parent' => {
'category_i18ns' => 'language'
},
},
}
);
}
sub with_account {
my ($self) = @_;
$self->search(
undef,{
prefetch => ['account'],
});
}
sub display {
my ($self) = @_;
$self->result_class('DBIx::Class::ResultClass::HashRefInflator');
my @return = $self->all;
return \@return;
}
When I call the chain this way:
my @categories = $self->db->resultset('Category')->with_translation($lang)->with_products($lang)->display;
DBIx :: Class generates a SELECT query containing only one WHERE clause:
SELECT [...] WHERE ( language.code = ? ): 'en'
what is expected according to the attributes and conditions that permit the rules described in the DBIx :: Class :: ResultSet documentation. But how can I generate a query with multiple WHERE clauses for each associated "language.code" column? For example, something like this:
SELECT [...] WHERE (( language.code = ? ) AND ( language_2.code = ? )): 'en', 'en'
As I understand it, the problem is that the search terms are concatenated during the chaining, so I need to get the current language alias and use it in search mode for each element of the chain, but DBIx :: Class doesn't seem to provide such Ability.
source to share
If you change your final version to this, it should work:
$self->db->resultset('Category')
->with_translation($lang)->as_subselect_rs
->with_products($lang)->as_subselect_rs
->display;
I think it will work. The problem is that it can abort the prefetch, I'm not sure ...
Another thing to keep in mind is that you pre-type two sets from manys. I believe it will be "Cartesian" and discard a huge dataset. The best option might be to do 2 queries and merge the result in perl.
source to share