[Dbix-class] column accessor namespace
Matt S Trout
dbix-class at trout.me.uk
Tue Jul 4 17:57:03 CEST 2006
Dave Howorth wrote:
> In February I wrote:
>> I'm looking at moving from CDBI to DBIC for a variety of reasons.
>>
>> One of the problems I have with CDBI is that it puts the column accessor
>> methods into the same namespace as its own class methods and
>> plugin-sourced methods. This combined with Perl's inability to
>> distinguish a class call from an instance call (except by adding code at
>> the start of every method) means that there can be conflicts between
>> column accessors and predefined methods. For example, my column
>> accessors called 'sequence' and 'primary' both caused problems.
>>
>> So I was wondering what DBIC does about this issue. From a quick scan of
>> the schemaintro doc, it looks like it has the same flaw. And a different
>> set of predefined methods to conflict with :) For example, I really do
>> have an accessor called 'belongs_to' :(
>
> I got a few suggestions in reply to this and recently I started to
> experiment with DBIC. It's been going really well. Thanks!
>
> Today I tried to connect to the database that has a table with a column
> called 'belongs_to'. It failed and because I'd forgotten about this
> issue I initially found the error message very confusing:
>
> DBIx::Class::Schema::Loader::connection(): Can't fetch data as class
> method at ./compare-db.pl line 76
>
> My code is just:
>
> use QD1::Schema;
> my $schema = QD1::Schema->connection($dsn, $dbuser, $dbpass);
>
> and my Schema module is just:
>
> package QD1::Schema;
> use base qw/DBIx::Class::Schema::Loader/;
> __PACKAGE__->loader_options(relationships => 1);
> 1;
>
> It would be nice if name clashes were detected and explicitly reported,
> so as to produce meaningful error reports like CDBI does :)
You might like to have a play with Schema::Loader's dump_to_dir method, which
would allow you to generate the DBIC definitions for stuff, and then tweak
appropriately (changing __PACKAGE__->belongs_to into an explicit
add_relationship call should get rid of that clash)
DBIC tends to overwrite methods rather than barfing like CDBI does since often
you *can* overwrite a substantial number of provided methods without it
failing. You could even, provided your "belongs_to" column doesn't need to be
a belongs_to rel itself, simply add_columns it as the last thing in the file :)
--
Matt S Trout Offering custom development, consultancy and support
Technical Director contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd. mst (at) shadowcatsystems.co.uk for more information
+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +
More information about the Dbix-class
mailing list