[CDBI] State of the DBIx::Class Class::DBI compatibility layer

Michael G Schwern schwern at pobox.com
Thu Feb 14 08:22:30 GMT 2008

Rod McChesney wrote:
>  > with the important caveat that relationships (has_a, has_many...) 
> have to be
>  > declared *after* all classes have been declared.
> Could you expand on that a little? Not sure exactly what it means. Does 
> it means that you can't define the relationships with the class, but 
> have to run the has_* statements after all the relevant classes have 
> been use'd somewhere? Seems like that would be a Bad Thing...

It's not as bad as I made it out to be.

If your classes are each in their own file everything should work as with 
Class::DBI.  If they're not, things get problematic.

In CDBI, you can declare multiple classes in one file like so:

	package My::Foo;
	use base qw(Class::DBI);

	My::Foo->columns( All => qw( id bar ) );
	My::Foo->has_a( bar => "My::Bar" );

	package My::Bar;
	use base qw(Class::DBI);

	My::Bar->columns( All => qw( id ) );

That works because CDBI doesn't do anything with the relationship until you 
use it.  But DBIC does.  has_a() will notice that My::Bar has not yet been 
declared and try to require My::Bar.  Since they're all in one file that won't 

If you're declaring multiple classes in one file, you need to do it like this:

	package My::Foo;
	use base qw(DBIx::Class::CDBICompat);

	My::Foo->columns( All => qw( id bar ) );

	package My::Bar;
	use base qw(Class::DBI);

	My::Bar->columns( All => qw( id ) );

	My::Foo->has_a( bar => "My::Bar" );

This is also problematic if you're generating the CDBI classes on the fly, say 
from some schema file.  You have to queue up the relationships until the end.

That part of CDBICompat was written by somebody else and I haven't messed with 
it.  I have some idea how to fix it, but AFAIK I'm the only one making serious 
use of CDBICompat.  And since most CDBI projects are one file, one class, one 
table I don't believe it to be pressing.

24. Must not tell any officer that I am smarter than they are, especially
     if it’s true.
     -- The 213 Things Skippy Is No Longer Allowed To Do In The U.S. Army

More information about the ClassDBI mailing list