Perl日記

日々の知ったことのメモなどです。Perlは最近やってないです。

Teng::Schema::Loaderを使ったらTengオブジェクトが返るので

その後Teng->newする必要なかった。
0.14から0.14_01での変更点みたい。


以下Amon2での使用の例。

MyApp.pm
use Teng;
use Teng::Schema::Loader;
use Test::DB;
my $schema;
sub db {
	my $self = shift;
	if ( !defined $self->{db} ) {
		my $conf = $self->config->{'DBI'}
			or die "missing configuration for 'DBI'";
		my $dbh = DBI->connect(@{$conf});
#		$schema ||= Teng::Schema::Loader->load(
		$self->{db} = Teng::Schema::Loader->load(
			namespace => 'MyApp::DB',
			dbh       => $dbh,
		);
#		$self->{db} = MyApp::DB->new(
#			dbh    => $dbh,
#			schema => $schema,
#		);
	}
	return $self->{db};
}

というかこうしないと、MyApp::DB(Teng)オブジェクトが作れない。

	$schema ||= Teng::Schema::Loader->load(
		namespace => 'MyApp::DB',
		dbh       => $dbh,
	);
	$self->{db} = MyApp::DB->new(
		dbh    => $dbh,
		schema => $schema,
	);
#=> Can't locate object method "prepare_from_dbh" via package "MyApp::DB"

$schemaは実はTengオブジェクトなので、作るとするなら

	$self->{db} = MyApp::DB->new(
		dbh    => $dbh,
		schema => $schema->schema,
	);

こうか。