Skip Menu |

This queue is for tickets about the DBIx-Class CPAN distribution.

Report information
The Basics
Id: 25011
Status: resolved
Priority: 0/
Queue: DBIx-Class

People
Owner: Nobody in particular
Requestors: mschwern [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: CDBICompat does not allow columns to be set before table.
In CDBI this is perfectly kosher. Foo->columns( Essential => qw(this that) ); Foo->table('foo'); But CDBICompat blows up Can't locate object method "result_source_instance" via package "Foo" at lib/DBIx/Class/CDBICompat/ColumnCase.pm line 17. The problem is table() is being used as a convenient place to initialize the table_class instance and stick it into the class' result_source_instance(). If that already existed, even if the object were empty, there would be no problem. A test is attached.
Subject: columns_before_table.t
#!/usr/bin/perl -w use strict; use Test::More; BEGIN { eval "use DBIx::Class::CDBICompat;"; plan $@ ? (skip_all => 'Class::Trigger and DBIx::ContextualFetch required') : (tests=> 2); } # Make sure that we can set up columns properly package State; use base 'DBIx::Class::Test::SQLite'; eval { State->columns(Essential => qw/Abbreviation Name/); State->table("State"); }; ::is $@, '', 'columns() can be called before table()'; ::is_deeply [sort map lc, State->columns("Essential")], [sort map lc, qw(abbreviation name)];
Here's a patch which makes it work. Its a little hackish. _init_result_source_instance() checks to see if there already is a result_source_instance() and if its for the current class (because it might have gotten one from a super class). If it is, it just returns it. If not, it make a new table class and plugs it in. I would prefer that result_source_instance() just did this but I wasn't able to pull it off.
=== lib/DBIx/Class/CDBICompat/ColumnCase.pm ================================================================== --- lib/DBIx/Class/CDBICompat/ColumnCase.pm (revision 27420) +++ lib/DBIx/Class/CDBICompat/ColumnCase.pm (local) @@ -14,7 +14,7 @@ sub add_columns { my ($class, @cols) = @_; $class->mk_group_accessors(column => @cols); - $class->result_source_instance->add_columns(map lc, @cols); + $class->_init_result_source_instance->add_columns(map lc, @cols); } sub has_a { === lib/DBIx/Class/ResultSourceProxy/Table.pm ================================================================== --- lib/DBIx/Class/ResultSourceProxy/Table.pm (revision 27420) +++ lib/DBIx/Class/ResultSourceProxy/Table.pm (local) @@ -4,6 +4,7 @@ use warnings; use base qw/DBIx::Class::ResultSourceProxy/; + __PACKAGE__->load_components(qw/AccessorGroup/); __PACKAGE__->mk_group_accessors('component_class' => 'table_class'); @@ -12,6 +13,32 @@ __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do # anything yet! +sub _init_result_source_instance { + my $class = shift; + + $class->mk_classdata('result_source_instance') + unless $class->can('result_source_instance'); + + my $table = $class->result_source_instance; + my $class_has_table_instance = ($table and $table->result_class eq $class); + return $table if $class_has_table_instance; + + $table = $class->table_class->new({ + name => undef, + result_class => $class, + source_name => undef, + }); + + $class->result_source_instance($table); + + if ($class->can('schema_instance')) { + $class =~ m/([^:]+)$/; + $class->schema_instance->register_class($class, $class); + } + + return $table; +} + =head1 NAME DBIx::Class::ResultSourceProxy::Table - provides a classdata table @@ -44,20 +71,7 @@ sub table { my ($class, $table) = @_; return $class->result_source_instance->name unless $table; - unless (ref $table) { - $table = $class->table_class->new({ - $class->can('result_source_instance') ? - %{$class->result_source_instance} : (), - name => $table, - result_class => $class, - source_name => undef, - }); - } - $class->mk_classdata('result_source_instance' => $table); - if ($class->can('schema_instance')) { - $class =~ m/([^:]+)$/; - $class->schema_instance->register_class($class, $class); - } + $class->_init_result_source_instance->name($table); } =head2 has_column
again, resolved by your patch :) (I'm sorry I didn't see these at the time, rt.cpan decided to not send me mail for quite a while)