http://www.perlmonks.org?node_id=1231265

nysus has asked for the wisdom of the Perl Monks concerning the following question:

I'm not sure if "subquery" is the appropriate term in my question. I'm new to DBIx. I'm using the WordPress::DBIC::Schema module.

I have the following module:

use Moose; use WordPress::DBIC::Schema; use namespace::autoclean; has 'schema' => (is => 'ro', isa => 'WordPress::DBIC::Schema', lazy => 1, builder => '_set_schema'); has 'rs' => (is => 'ro', isa => 'DBIx::Class::ResultSet', lazy => 1, builder => '_set_resultset'); has 'results' => (is => 'ro', isa => 'WordPress::DBIC::Schema::ResultSet::WpPost', lazy => 1, builder => '_set_results'); sub _set_schema { my $s = shift; my $db = $s->subdomain; my $user = $s->_db_user; my $pass = $s->_db_pass; my $schema = WordPress::DBIC::Schema->connect("dbi:mysql:database=$d +b", $user, $pass); return $schema; } sub _set_resultset { my $s = shift; return $s->schema->resultset('WpPost'); } sub _set_results { my $s = shift; return $s->rs->search({ post_type => 'nav_menu_item' }, { order_by => { -asc => 'id' } }); } sub dump_meta { my $s = shift; my %ids; while ( my $thing = $s->results->next ) { logd $thing->post_title; my $id = $thing->id; foreach my $meta ($thing->metas) { $ids{$id}{$meta->meta_key} = $meta->meta_value; } } $s->results->reset; logd \%ids; } sub get_title { my $s = shift; my $id = shift; my $results = $s->results->find($id); my $title = $results->post_title; if ($title) { return $title; $s->results->reset; } ############# Trying to figure out this part, does not work ######## +############ my $post_id = $results->metas->select('_menu_item_object_id'); print $post_id . "\n"; }

So the dump_meta function works fine. You can see there how the meta values associated with a post are printed out. I want to try to access a single meta_value for an associated meta_key in the get_title method. I could loop through all the values like I did in dump_meta and pull it out that way but I'm wondering if there is a better way. Thanks!

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: How to do a DBIx subquery?
by nysus (Parson) on Mar 14, 2019 at 17:20 UTC

    After a lot of trial and error, I stumbled on a solution:

    my $post_id = $results->metas->find( {meta_key => '_menu_item_object_i +d'} )->meta_value;

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Re: How to do a DBIx subquery?
by nysus (Parson) on Mar 14, 2019 at 16:17 UTC

    I think I need to do a prefetch as illustrated here

    I tried:

    my $results = $s->results->find($id, { prefetch => ['metas'] } ); my $post_id = $results->_menu_item_object_id;

    I just got an error, though: Can't locate object method "_menu_item_object_id" via package "WordPress::DBIC::Schema::Result::WpPost"

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks