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)
_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
Searchable Archive: http://www.mail-archive.com/[email protected]/