[Dbix-class] Issues getting Catalyst::Model::DBIC::Schema to layer lib/MyApp/Model/Foo.pm on top of generated version

Scotty Allen lists at scottyallen.com
Sat Feb 25 23:58:19 CET 2006


So, I'm gradually trying to migrate from CDBI to DBIC for a Catalyst
project.  I'd like to use Catalyst::Model::DBIC::Schema in a similar
fashion to how I've been using Catalyst::Model::CDBI.  Specifically, I'd
like the loader to be in charge of loading the columns and such, and yet
also be able to tweak relationships and add additional methods via a
class in lib/Model.

I noticed that the changelog for 0.07 of C::M::DBIC::Schema says:

        - Added ->require for source classes, so that you can
          layer in lib/MyApp/Model/Foo/Bar.pm on top of the
          generated MyApp::Model::Foo::Bar.

However, either this is borked, or it just doesn't work like I expect it
to.

I'm running into a couple of issues.  I've setup a test catalyst project
that's laid out as follows: There's one table, 'foo', which is loaded
via a Schema::Loader MyTest::SchemaLoader::DBIC.  There is also an
associated MyTest::Model::DBIC.  This bit seems to work as expected.

However, when I add in MyTest::Model::DBIC::Foo with the following code:

        package MyTest::Model::DBIC::Foo;
        
        die "here";
        
        sub foo {
            die "foo";
        }
        
        1;

it never hits the 'die "here"', which implies to me it's never actually
getting loaded.  In C::M::DBIC::Schema there's the code:

  foreach my $moniker ($self->schema->sources) {
        my $classname = "${class}::$moniker";
        $classname->require;

Presumably that's where the magic is supposed to happen.  However, if
you inspect %INC right before the require, MyTest/Model/DBIC/Foo.pm is
already listed, and thus, it sounds to me like require returns without
actually doing anything.

If I add the following hack, it works as expected:

        my $filename = $classname;
        $filename =~ s#::#/#g;
        $filename .= '.pm';
        delete $INC{$filename};

However, now the problem is, once I remove 'die "here"' from
MyTest::Model::DBIC::Foo, the following test fails

        use Test::More tests => 1;
        use_ok( Catalyst::Test, 'MyTest' );
        
        warn ref(MyTest->model('DBIC::Foo'));
        MyTest->model('DBIC::Foo')->foo;
        
with the error "Can't locate object method "foo" via package
"DBIx::Class::ResultSet" at t/model_DBIC.t line 7".

That looks to me like it might be an issue with the code:

        *{"${classname}::ACCEPT_CONTEXT"} = sub {
            shift;
            shift->model($model_name)->resultset($moniker);
        }

in C::M::DBIC::SChema.

Anyone have any thoughts?  Am I trying to use this wrong, or is there
actually an issue here?

Thanks,

Scotty
--
scotty at scottyallen.com




More information about the Dbix-class mailing list