[Catalyst] Changing Model attributes in ACCEPT_CONTEXT
James R. Leu
jleu at mindspring.com
Wed Jan 24 22:30:00 GMT 2007
Disclaimer: I'm new to catalyst. I've searched the archive and was unable
to find an answer to my question. If I've overlooked a source, please point
me to the URL.
I'm working with a data source that requires the database name and table
name to change based on the active user.
Here is a simplified example:
The following database reside within a common mysql instance.
Database: config_alpha
Table: common
Database: config_beta
Table: common
Database: data
Table: foo_alpha
Table: foo_beta
Depending which user logged in 'alpha' or 'beta' they would
connect to the appropriate 'config' database. In addition they are
connected the the 'data' database. They would have permission
to see data in one of the 'foo' tables. I would like to define a has_many
relationship between entries in the 'common' and the 'foo' table.
After talking with the DBIx::Class list I implemented template schemas for
'Config' and 'Foo' which I use for defining the relationships. I then need to
update the 'connect_info' and table name for each users session. Here is how
I do that:
package MyApp::Model::Config
use base 'Catalyst::Model::DBIC::Schema';
...
sub ACCEPT_CONTEXT {
my ($self, $c) = @_;
if ($c->user_exists) {
my $name = $c->user->username;
$self->{connect_info} = [
'DBI:mysql:config_'.$name,
'username',
'password',
];
my $schema = $self->clone();
my $table = 'data.foo_'.$c->user->username;
$schema->source('Foo')->name($table);
$schema->connection(@{$self->{connect_info}});
$self->schema($schema);
}
return $self;
}
1;
This seems to work, but it feels wrong. Is there a better way
to go about changing the Model attributes on the fly per user.
--
James R. Leu
jleu at mindspring.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070124/5c44210d/attachment.pgp
More information about the Catalyst
mailing list