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

I have been able to get save to work, but fetch still doesn't work. Hopefully someone can suggest an avenue for me to follow.

I am accessing a Postgres database, and the code

my $user = MyUserClass->new( {u_name=>"mrwong",u_password=>"jshdghjashjd"}); $user->save;
works fine .. a new user is created in the database. When I try to fetch a user, either using fetch or fetch_group I get what appears to be an internal error in SPOPS.

I am assuming that I have the configuration wrong, but after some time spent reading the man pages, I'm not sure what mistake I'm making.

test1.pl:

#!/usr/bin/perl -w use SPOPS; use MyUserClass; { SPOPS::set_global_debug( debug => 1 ); my $otherUser = MyUserClass->fetch(4); foreach my $key ( keys %$otherUser ) { print "Key $key: $otherUser->{$key}\n"; } }
MyUserClass.pm:
package MyUserClass; use strict; use SPOPS::Initialize; my $spops = { 'users' => { class => 'MyUserClass', field => [ 'u_name', 'u_password' ], isa => [qw/Datasource SPOPS::DBI::Pg SPOPS::DBI/], id_field => ['u_id'], base_table => 'users', field_discover => 'no', }, }; { SPOPS::Initialize->process( { config => $spops } ); } sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; my $vars = shift; foreach my $key ( keys %{$vars} ) { $self->{$key} = $vars->{$key}; } bless $self, $class; return $self; }
DataSource.pm:
package Datasource; use strict; use DBI; my ($dbh); sub global_datasource_handle { my ($class) = @_; return $dbh if ($dbh); $dbh = DBI->connect( 'DBI:Pg:dbname=xxx', 'xxx', 'xxx', { RaiseError => 1, PrintError => 0 } ); unless ($dbh) { die "Cannot create database handle! Error: $DBI::errstr\n"; } return $dbh; } 1;
The error that I get:
SPOPS::ruleset_process_action (323) >> Trying to process pre_fetch_act +ion for a MyUserClass type of object SPOPS::clear_all_loaded (399) >> Clearing all fields to unloaded for o +bject class MyUserClass Can't use an undefined value as a HASH reference at /usr/lib/perl5/sit +e_perl/5.8.0/SPOPS.pm line 400. Use of uninitialized value in concatenation (.) or string at /usr/lib/ +perl5/site_perl/5.8.0/SPOPS.pm line 173.
Any suggestions, comments would be appreciated. Thanks.

--t. alex
Life is short: get busy!

Replies are listed 'Best First'.
Re: SPOPS fetch problems
by lachoy (Parson) on Jun 27, 2003 at 18:36 UTC

    The problem is you're implementing new(). This is already implemented in SPOPS.pm and it does a lot of initialization work there. Additionally, you don't need to put the initialization (etc.) into a class 'MyUserClass' since that class is generated for you when you call SPOPS::Initialize->process.

    Chris
    M-x auto-bs-mode

      Thanks, that worked, of course. Guess I better re-read the documentation.

      --t. alex
      Life is short: get busy!
Re: SPOPS fetch problems
by talexb (Chancellor) on Jun 27, 2003 at 16:49 UTC

    Sorry, the version string is

    # $Id: SPOPS.pm,v 3.15 2003/06/11 04:45:55 lachoy Exp $
    I think I'm going to make up a list of Things To Remember When You're Reporting A Problem You Think You Have With A Module. So far I've got
    • Include all relevant files (check)
    • Use a READMORE tag (you goof) if the post is more than twenty lines long (oops)
    • Include version information (oops)

    --t. alex
    Life is short: get busy!
Re: SPOPS fetch problems
by talexb (Chancellor) on Jun 27, 2003 at 18:21 UTC

    So anyway I tried the examples that are provided with SPOPS, and after editing My::Common with my choices (Postgres, path to database) was able to install all of the data. Then I tried doing step 6, fetch_all.pl, and that worked fine. Step 7, stock_doodads.pl, failed with the error

    [alex@won egSPOPS]$ perl -w stock_doodads.pl Added 'set_creator' to My::Doodad Use of uninitialized value in string eq at /usr/lib/perl5/site_perl/5. +8.0/SPOPS/Secure/DBI.pm line 68. Can't call method "id" on an undefined value at My/Doodad.pm line 71.
    Since the rest of the test items (to step 11) all do things with 'doodad' I haven't bothered with the rest of the steps, but since the example was able to get 'fetch' work, I'm going to go over that code and see what the differences are between that and my original code (root node).

    --t. alex
    Life is short: get busy!