Hello,

I'm hoping some kind person will be able to help me progress to get
relationships working as I've tussled with this problem for quite some time.
I've set up 2 tables to test with.  I've tried to keep it simple and renamed
everything to adhere to default conventions as given in the docs.  So it
should all be simple stuff.  Here is my database setup code:

use Rose::DB::Object::Loader;
my $loader = Rose::DB::Object::Loader->new(
        db           => GARD::DB->new(),
        class_prefix => 'GARD::',
        include_tables => ['books', 'booktypes']
        );

$loader->post_init_hook(
        sub {
        my $meta = shift;
        # For class My::Product, try to load My::Product::Extra
        my $extra_pm = $meta->class .'::Extra';
        eval "require $extra_pm";
        });

my @classes = $loader->make_classes;



The following works fine if the "require_objects" is not included.

-------------------------------------
my $searchspec = {query => [title => { like => '%chess%' }], require_objects
=> ['booktype']};
my $books = GARD::Book::Manager->get_books(%$searchspec);
print @$books." results found<br>";
-------------------------------------

But as it stands, it produces the following error:

-------------------------------------
get_objects() - Can't locate object method "booktype" via package
"GARD::Book" at C:/perl/site/lib/Rose/DB/Object.pm line 1428
        Rose::DB::Object::AUTOLOAD('GARD::Book=HASH(0x3f27e18)',
'GARD::Booktype=HASH(0x3f27f8c)') called at
C:/perl/site/lib/Rose/DB/Object/Manager.pm line 2464
        eval {...} called at C:/perl/site/lib/Rose/DB/Object/Manager.pm line 
1512
        Rose::DB::Object::Manager::get_objects('GARD::Book::Manager',
'require_objects', 'ARRAY(0x3edbef4)', 'query', 'ARRAY(0x3edc554)',
'object_class', 'GARD::Book') called at
C:/perl/site/lib/Rose/DB/Object/Manager.pm line 201
        Rose::DB::Object::Manager::__ANON__('GARD::Book::Manager',
'require_objects', 'ARRAY(0x3edbef4)', 'query', 'ARRAY(0x3edc554)') called
at db.pl line 320
        main::search_results('books', 'HASH(0x3e10ee8)') called at db.pl line 56
        main::bread_db() called at
D:/Information/InetPub/gamesboard/cgi-bin/gard.cgi line 31
-------------------------------------

In trying to sort this out I've found two previous discussions that cite a
similar problem.  The first was due to the package in the err message not
existing but it does seem to - see below.  The second was due to an
initialise not being done after the add_relationships in the Extra Class.
But I believe I do this.  Here's the code from GARD/Book/Extra.pm:


-------------------------------------
package GARD::Book::Extra;
GARD::Book->meta->add_relationships(
        booktype => {
                type => 'many to one',
                class => 'GARD::Booktype',
                column_map => { booktypecode => 'code' }
                }
        );
GARD::Book->meta->initialize(preserve_existing => 1);
-------------------------------------


Here below is the output from showing all the classes that are set up
immediately after the make_classes command:


package GARD::Book;

use strict;

use base qw(GARD::DB::Object::AutoBase1);

__PACKAGE__->meta->setup
(
  table   => 'books',

  columns =>
  [
    uid               => { type => 'integer', not_null => 1 },
    title             => { type => 'varchar', default => '', length => 255,
not_null => 1 },
    author            => { type => 'varchar', default => '', length => 255,
not_null => 1 },
    publisher         => { type => 'varchar', default => '', length => 255,
not_null => 1 },
    publishyear       => { type => 'varchar', default => '', length => 16,
not_null => 1 },
    origpublished     => { type => 'varchar', default => '', length => 16,
not_null => 1 },
    firstedpublished  => { type => 'varchar', default => '', length => 16,
not_null => 1 },
    edition           => { type => 'integer', default => '0', not_null =>
1 },
    isbn              => { type => 'varchar', default => '', length => 63,
not_null => 1 },
    booktypecode      => { type => 'enum', default => '', not_null => 1,
values => [ '', 'B', 'C', 'M', 'D', 'A' ] },
    gamepages         => { type => 'varchar', default => '', length => 32,
not_null => 1 },
    allgamesenteredby => { type => 'varchar', default => '', length => 8,
not_null => 1 },
    oldcomment        => { type => 'blob', default => '', length => 65535,
not_null => 1 },
    history           => { type => 'enum', default => '', not_null => 1,
values => [ '', 'Y' ] },
    coverage          => { type => 'enum', default => '', not_null => 1,
values => [ '', 'all', 'think', 'skill', 'thinksp', 'skillsp', 'misc',
'pub' ] },
    submittercode     => { type => 'varchar', default => '', length => 8,
not_null => 1 },
  ],

  primary_key_columns => [ 'uid' ],

  relationships =>
  [
    booktype =>
    {
      class      => 'GARD::Booktype',
      column_map => { booktypecode => 'code' },
      type       => 'many to one',
    },
  ],
);

1;

package GARD::Book::Manager;

use base qw(Rose::DB::Object::Manager);

use GARD::Book;

sub object_class { 'GARD::Book' }

__PACKAGE__->make_manager_methods('books');

1;

package GARD::Booktype;

use strict;

use base qw(GARD::DB::Object::AutoBase1);

__PACKAGE__->meta->setup
(
  table   => 'booktypes',

  columns =>
  [
    uid         => { type => 'integer', not_null => 1 },
    code        => { type => 'character', default => '', length => 1,
not_null => 1 },
    description => { type => 'varchar', default => '', length => 64,
not_null => 1 },
  ],

  primary_key_columns => [ 'uid' ],

  relationships =>
  [
    books =>
    {
      class      => 'GARD::Book',
      column_map => { code => 'booktypecode' },
      type       => 'one to many',
    },
  ],
);

1;

package GARD::Booktype::Manager;

use base qw(Rose::DB::Object::Manager);

use GARD::Booktype;

sub object_class { 'GARD::Booktype' }

__PACKAGE__->make_manager_methods('booktypes');

1;


Thanks for any advice,

James.


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to