Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Querying data with and & or when using DBIx::Class

by phildeman (Acolyte)
on Sep 17, 2013 at 13:09 UTC ( #1054435=note: print w/ replies, xml ) Need Help??


in reply to Re: Querying data with and & or when using DBIx::Class
in thread Querying data with and & or when using DBIx::Class

Thanks for the help. I have done reading, but what I have tried was giving me errors. I had something similar to your first to examples.

method get_my_mixedskills( $id, $color1, $color2 ) {
   my $whrcls;
   if(!$color2){
       $whrcls = "ID => $id, color => $color1";
   }
   else {
       $whrcls = "-and => [
               -or => [ color => $color1, color => $color2, ],
               ID => $id, ], "
    }
    my @myskills_objs = $schema->resultset( 'TblSkillSelected' )->search({ $whrcls },
                   { order_by => 'SID' });
}

But using this I got an error, listed in the previous post.

Yours script is slightly different. I will try that.

Thanks again


Comment on Re^2: Querying data with and & or when using DBIx::Class
Re^3: Querying data with and & or when using DBIx::Class
by Your Mother (Canon) on Sep 17, 2013 at 14:31 UTC

    So, what's going on here is you're using a string in place of a(n anonymous) hash.

    $whrcls = "ID => $id, color => $color1";

    Should be-

    $whrcls = { ID => $id, color => $color1 }; # OR something like my %params = ( ID => $id, color => $color1 ); $whrcls = \%params;

    It looks like you might be using some global variables and non-standard code structures (method instead of plain sub). Globals are going to tend to come back and bite you. Definitely avoid them. Non-standard stuff like method signatures has a dearth of examples and help and can come with really deep and difficult edge case bugs. I don't want to put you off that necessarily but if you're having a little trouble with Perl syntax it might not be the best place to jump in.

      Thanks again for your response.

      I am using MooseX.

      The issue is resolved. Instead of building the WHERE clause in $whrcls, I simply added the complete DBIx::Class search within the if/else statement:

      method get_colors( $id, $color1, $color2 ){
          my @mycolor_objs;
          if(!$color2){
              @mycolor_objs = $schema->resultset( 'TblColors' )->search({ ID => $id, color => $color1 },
                                      { order_by => 'ID' });
          }
          else {
              @myscolor_objs = $schema->resultset( 'TblColors' )->search({ ID => $id,
                                      -or => [ color => $color1, color => $color2 ] },
                                      { order_by => 'ID' });
          }
      }

      I am unsure at this moment, but it seems DBIx::Class is a little sensitive when using a scalar to represent the hash being passed as the WHERE clause. Thanks again. When I have a little more time, I will research building WHERE clauses with scalars.

        it seems DBIx::Class is a little sensitive when using a scalar...

        Don't confuse scalar for string just because a scalar often holds strings. They aren't the same. A hash ref can be put into a scalar ( my $hash_ref = \%some_hash or my $x = {}) and hash refs are the only kind of argument DBIC takes for its search and options clauses: never strings. There is no way to (safely and reliably) separate out the bind values and parse the intention of the tree from a string. Compare your original with what is necessary-

        print "ID => $id, color => $color1", $/; print { ID => $id, color => $color1 }, $/;
      Thanks again for your response.

      I am using MooseX.

      The issue is resolved. Instead of building the WHERE clause in $whrcls, I simply added the complete DBIx::Class search within the if/else statement:

      method get_colors( $id, $color1, $color2 ){
          my @mycolor_objs;
          if(!$color2){
              @mycolor_objs = $schema->resultset( 'TblColors' )->search({ ID => $id, color => $color1 },
                                      { order_by => 'ID' });
          }
          else {
              @myscolor_objs = $schema->resultset( 'TblColors' )->search({ ID => $id,
                                      -or => [ color => $color1, color => $color2 ] },
                                      { order_by => 'ID' });
          }
      }

      I am unsure at this moment, but it seems DBIx::Class is a little sensitive when using a scalar to represent the hash being passed as the WHERE clause.

      Thanks again. When I have a little more time, I will research building WHERE clauses with scalars.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054435]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-08-21 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (142 votes), past polls