[Dbix-class] search using multiple values for the same column
Will Hawes
info at whawes.co.uk
Wed Jul 5 19:07:56 CEST 2006
Fernan Aguero wrote:
> Hi,
>
> I'm having trouble with this:
>
> TABLE items (item_id, item)
> TABLE features (feature_id, feature)
> TABLE item_features (item_id, feature_id)
>
> the latter being a join/linking table, so that any item can
> have many features and any feature can be related to many
> items.
>
> then I do something like this:
>
> my $dbix = My::Schema->connect( ... );
>
> my $item = $dbix->resultset('Items');
> my $feat = $dbix->resultset('Features');
> my $itfeat = $dbix->resultset('ItemFeatures');
>
> my $itemrs = $item->search( { item_id => $item_id } );
> my $itemfeatrs = $itfeat->search( { item_id => $item_id } );
>
> # now I want to get all feature data for the feature_ids I
> # obtained in the last query
>
> my $features = [];
> while (my $itemfeature = $itemfeatrs->next() ) {
> push @{$features}, $itemfeature->feature_id;
> }
>
> # and now I should do the following
> my $featrs = $feat->search( { feature_id => $features } );
>
> except that the problem is before this step, because
> $itemfeature->feature_id
> is not returning a scalar (the id) but a HASH
>
>
> All classes were generated by Schema::Loader
> (relationships => 1)
>
> DBIx-Class-0.06003
> DBIx-Class-Schema-Loader-0.03003
>
>
> Is this a bug? Am I doing something wrong?
>
> Thanks in advance,
>
> Fernan
This is because $itemfeature->feature_id gets auto-inflated to a
"Features" object.
If you want just the value, you could access it like this:
$itemfeature->feature_id->feature_id
Alternatively (and possibly more efficiently), you could use
get_column() to obtain the column value, bypassing inflation completely:
$itemfeature->get_column('feature_id')
Assuming you are able to alter the database I would also be tempted to
say structure your tables like this instead to avoid confusion:
TABLE items (id, item)
TABLE features (id, feature)
TABLE item_features (item, feature)
More information about the Dbix-class
mailing list