次の操作を行いパラメータを指定して呼び出すにはregister_source
package My::Schema::User;
use base qw/DBIx::Class/;
# ->load_components, ->table, ->add_columns, etc.
# Make a new ResultSource based on the User class
my $source = __PACKAGE__->result_source_instance();
my $new_source = $source->new($source);
$new_source->source_name('UserFriendsComplex');
# Hand in your query as a scalar reference
# It will be added as a sub-select after FROM,
# so pay attention to the surrounding brackets!
$new_source->name(\<<SQL);
(SELECT u.* FROM user u
INNER JOIN user_friends f ON u.id = f.user_id
WHERE f.friend_user_id = ?
UNION
SELECT u.* FROM user u
INNER JOIN user_friends f ON u.id = f.friend_user_id
WHERE f.user_id = ?)
SQL
# Finally, register your new ResultSource with your Schema
My::Schema->register_source('UserFriendsComplex' => $new_source);
を使用することができません、DBIx ::クラスのコンテキストでストアドプロシージャを実行するための合理的な方法はありません。私の知る限り、この問題を回避するに最も近いものが弱いスープであるデータベース・ハンドル、取得するには、「ORMを使用して」され
:
my @results = $schema->storage->dbh_do(sub{
my ($storage, $dbh, @args) = @_;
my $sth = $dbh->prepare('call storedProcNameFooBar()');
my @data;
$sth->execute();
while(my $row = $sth->fetchrow_hashref){
push @data, $row;
}
return @data;
},());
を[ http://metacpan.org/pod/DBIx::Class::Storage::DBI#dbh_doで詳細を見る]
... ORMの恩恵を受けることはありません。
'DBIx :: Class :: Manual :: Cookbook'ドキュメントセクション 'データベース関数やストアドプロシージャを使う' @stevenlはMS SQL Serverには役に立たないと指摘していますが、ストアドプロシージャにアクセスできないSELECT文を使用します。 – LeeGee
でもMysqlでもありませんし、私の推測もOracleではありません。私はDBIx:Classのマニュアル作成者がそれを作っているのかどうか疑問です。 – djsadinoff
実際、期待される行動は何でしょうか? DBICは、ストアド・プロシージャまたはストアド・ファンクションによって戻されたデータをどのResultSetで関連付けられるかをどのように知っていますか?私は、長さが例であるため、著者はSQL関数のように '関数'を意味すると考えています。それはどのように 'ストアドプロシージャ'が追加されたか説明していません。 – LeeGee