[CDBI] self-referential has_many possible with subclass?

Charles Bailey bailey.charles at gmail.com
Sun Aug 14 22:36:45 BST 2005

On 8/12/05, Phil Mitchell <seasprocket at gmail.com> wrote:
> In MyApp::Page ...
> MyApp::Page->has_many( comments => 'MyApp::Page::Comment );
> And in MyApp::Page::Comment...
> MyApp::Page::Comment->has_a( refers_to => MyApp::Page );
> Comment is a subclass of Page.
> But the compiler dies with:
> Error:  Can't locate object method "meta_info" via package
> "MyApp::Page::Comment" at
> /usr/local/share/perl/5.8.4/Class/DBI/Relationship/HasMany.pm line 28,
> <DATA> line 22008.
> Is this just not possible?

It is possible, but you need to be careful about order of statements. 
The problem here is likely is that CDBI's relationship innards try to
C<require> the target class's definition as the relationship is set
up.  In this case, a partially-set-ujp MyApp::Page tries to establish
a has_many relationship with MyApp::Page::Comment, which in turn tries
to require MyApp::Page again as it sets up the has_a.  This should
work as long as you're careful to say:

    package MyApp::Page;
    __PACKAGE__->has_many( comments => 'MyApp::Page::Comment');

    package My:App::Page::Comment;
    MyApp::Page::Comment->has_a( refers_to => 'MyApp::Page');

In other words, let MyApp::Page call out to MyApp::Page::Comment, but
defer setting up the "reverse" relationship until both classes have
otherwise finished initializing themselves.

Charles Bailey
Lists: bailey _dot_ charles _at_ gmail _dot_ com
Other: bailey _at_ newman _dot_ upenn _dot_ edu

More information about the ClassDBI mailing list