[Dbix-class] changing view definition

Dave Howorth dhoworth at mrc-lmb.cam.ac.uk
Thu May 24 16:12:00 GMT 2012


I wrote:
> Is it possible to change the definition of a
> DBIx::Class::ResultSource::View based on the particular database to
> which a connection is made?
> 
> I have a result class that is a union of several tables. Unfortunately,
> I now have another database to which I must connect that has an
> additional table needing to be included in the union. Is it possible to
> do this by connecting to the database and then changing the result class
> somehow to use the expanded union?
> 
> My class initializes with:
> 
> __PACKAGE__->result_source_instance->is_virtual(1);
> __PACKAGE__->result_source_instance->view_definition($my_select_union_sql);
> 
> I've tried adding a call later, once I know which database is in use:
> 
> __PACKAGE__->result_source_instance->view_definition($my_new_select_union_sql);
> 
> but DBIC still uses the original select SQL when querying the database.
> 
> Is there some other way of changing the definition of the view?

OK, so I have a tentative answer.

Accessing __PACKAGE__->result_source_instance seems to have no effect
once things have been set up, because 'magic' stashes the instance away.
To find it again, you have to go through a resultset or the schema, and
an invocation like:

  $result_set->result_source->view_definition($my_new_select_union_sql);

appears to have the desired effect of changing the view definition.

If anybody can confirm that this is OK, or warn me that evil things will
happen, I'd appreciate it.

I still find it hard to get my head around the structure of DBIx::Class :(

Cheers, Dave



More information about the DBIx-Class mailing list