Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to do a DBIx subquery?

by nysus (Vicar)
on Mar 14, 2019 at 15:59 UTC ( #1231265=perlquestion: print w/replies, xml ) Need Help??
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 (Vicar) 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 (Vicar) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1231265]
Approved by Athanasius
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2019-03-20 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (101 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!