[Dbix-class] add_column oddness - what am I missing ? (a newbie question)]

Christopher H. Laco claco at chrislaco.com
Mon Jun 18 16:03:53 GMT 2007


Richard Robinson wrote:
> On Mon, Jun 18, 2007 at 09:32:22AM +0100, Jess Robinson wrote:
>> On Sat, 16 Jun 2007, Richard Robinson wrote:
>>
>>> Since I don't know in advance what keys it might meet, I can't create a=
ll
>>> the possible columns beforehand and give a complete list to the
>>> __PACKAGE__->add_columns startup call, I have to deal with them on the =
fly.
>>> On startup, look at the state of the sql table to see what columns it d=
oes
>>> contain, and on new input create new columns in it as necessary ...
>> I suspect it's to do with how you're doing that ->add_columns calling at =

>> runtime, it would be quite helpful if you posted code to go with your =

>> problems ;)
> =

> Yes, I probably should have, sorry. It was just getting wordier than I li=
ked
> already ...
> =

> =

> in the table module :-
> package EditTunesDB::TuneTable;
> use base qw/DBIx::Class/;
> #stuff
> __PACKAGE__->table('abctable');
> __PACKAGE__->add_columns(qw/id owner_id etc/);  # not including 'T'
> =

> =

> and at runtime (error-checking etc removed for clarity) :-
> =

> my $result =3D $c->model('EditTunesDB::TuneTable');
> my $src =3D $result->result_source;
> my $dbh =3D $src->storage->dbh;
> my $q =3D $dbh->prepare("SELECT * FROM abctable;");
> $q->execute();
> for (my $n =3D 0; $n < $q->{NUM_OF_FIELDS}; $n++)
> { my $name =3D $q->{NAME}->[$n];
>   next if ( $src->has_column($name) );
>   $src->add_column( $name );
>   # column 'T' exists and is seen here.
> }
> =

> # and test
> my $row =3D $result->find({id=3D>1});
> $c->log->debug("get_column(id) " . $row->get_column('id') );
> $c->log->debug("get_column(T) "  . $row->get_column('T') );
> $c->log->debug("->id " . $row->id );
> $c->log->debug("->T "  . $row->T );
> =

> and the 1st 3 print okay, the 4th throws an exception "Can't locate object
> method "T" via package "EditTunes::Model::EditTunesDB::TuneTable".
> =

> =

> This is being called from code behind a Catalyst controller action; which=
 is
> another indication of a horrible lack of understanding - it only needs to=
 be
> called once, but I haven't been able to find a suitable entrypoint. (I'm
> reading all the FMs I can find as fast as I can digest them, okay ?)
> =


Sorry. I'm late to the party. I add/remove columns at runtime in Handel.
To make that work, I had to add columns to the source AND to the class:

$schema->source('Foo')->add_column
$schema->class('Foo')->add_column

If would work for me no other way, which isn't surprising because the
original intent of add_column was not to do it at runtime.

In your loop above, try adding to the class as well just to see if it
makes a difference.

-=3DChris

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 187 bytes
Desc: OpenPGP digital signature
Url : http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20070618/b8=
580001/signature.pgp


More information about the Dbix-class mailing list