DBIx-Class-0.07006 > DBIx::Class::Manual::Component

名前

DBIx::Class::Manual::Component - DBIx::Class コンポーネントを開発する

コンポーネントとは何か

A component is a module that can be added in to your DBIx::Class classes to provide extra functionality. A good example is the PK::Auto component which automatically retrieves primary keys that the database itself creates, after the insert has happened.

DBIx::Classのクラスに付加的な機能を提供するために追加するモジュールのことです。 好例としては、PK::Autoコンポーネントがあります。PK::Autoは、insertの後に、 データベース自身が、作ったプライマリキーを取り出します。

使い方

Components are loaded using the load_components() method within your DBIx::Class classes.

コンポーネントは、自分のDBIx::Class クラスの中に、 load_components() メソッドを使って、ロードします。

  package My::Thing;
  use base qw( DBIx::Class );
  __PACKAGE__->load_components(qw/ PK::Auto Core /);

Generally you do not want to specify the full package name of a component, instead take off the DBIx::Class:: part of it and just include the rest. If you do want to load a component outside of the normal namespace you can do so by prepending the component name with a +.

たいてい、コンポーネントの完全なパッケージ名を指定したくはないでしょうから、 その代わりに、DBIx::Class:: の部分を外して、残りの部分だけを含めます。 通常の名前空間以外のコンポーネントをロードしたいなら、 コンポーネント名の前に + を付けてください。

  __PACKAGE__->load_components(qw/ +My::Component /);

Once a component is loaded all of it's methods, or otherwise, that it provides will be available in your class.

コンポーネントがロードされると、全てのコンポーネントが提供する メソッドが自分のクラスで有効になります。

The order in which is you load the components may be very important, depending on the component. The general rule of thumb is to first load extra components and then load core ones last. If you are not sure, then read the docs for the components you are using and see if they mention anything about the order in which you should load them.

コンポーネントをロードする順番は非常に重要です。コンポーネントに依存します。 一般的な経験では、まず付加的なコンポーネントをロードし、それから、 コアなコンポーネントを最後にロードします。よくわからなければ、 使っているコンポーネントのドキュメントを読み、 ロードすべき順番について何か書かれているかを見てください。

コンポーネントを作る

Making your own component is very easy.

自分自身のコンポーネントを作るのはとても簡単です。

  package DBIx::Class::MyComp;
  use base qw(DBIx::Class);
  # Create methods, accessors, load other components, etc.
  1;

When a component is loaded it is included in the calling class' inheritance chain using Class::C3. As well as providing custom utility methods, a component may also override methods provided by other core components, like DBIx::Class::Row and others. For example, you could override the insert and delete methods.

コンポーネントがロードされると、呼び出しているクラスの、 Class::C3を使った、継承チェーンに含まれます。 カスタムユーティリティメソッドを提供するのと同様に、 コンポーネントが、他のコアコンポーネントで提供されている メソッドを上書きするかもしれません。DBIx::Class::Rowや その他のコンポーネントのように。 例えば、insert と delete メソッドをオーバーライドできます。

  sub insert {
    my $self = shift;
    # Do stuff with $self, like set default values.
    return $self->next::method( @_ );
  }
  
  sub delete {
    my $self = shift;
    # Do stuff with $self.
    return $self->next::method( @_ );
  }

Now, the order that a component is loaded is very important. Components that are loaded first are the first ones in the inheritance stack. So, if you override insert() but the DBIx::Class::Row component is loaded first then your insert() will never be called, since the DBIx::Class::Row insert() will be called first. If you are unsure as to why a given method is not being called try printing out the Class::C3 inheritance stack.

こうなると、コンポーネントをロードする順番はとても重要です。 最初にロードされるコンポーネントは、継承スタックの最初のものです。そのため、 insert() をオーバーライドしたとして、DBIx::Class::Rowコンポーネントが最初に ロードされると、自分のinsert()は、決して呼ばれません。DBIx::Class::Row insert() が最初に呼ばれてしまいます。あたえたメソッドが呼ばれないかよくわからなければ、 Class::C3 の継承スタックを出力してみてください。

  print join ', ' => Class::C3::calculateMRO('YourClass::Name');

Check out the Class::C3 docs for more information about inheritance.

継承についてより詳しくは、Class::C3のドキュメントをチェックしてください。

存在するコンポーネント

付加的なもの

These components provide extra functionality beyond basic functionality that you can't live without.

下記のコンポーネントは基本的な機能を越えた、 これ無しでは生きていけないような、付加的な機能を提供します。

DBIx::Class::CDBICompat - Class::DBI Compatibility layer.

DBIx::Class::CDBICompat - Class::DBI 互換レイヤ。

DBIx::Class::FormTools - Build forms with multiple interconnected objects.

DBIx::Class::FormTools - 複数の関連するオブジェクトでフォームを作る。

DBIx::Class::HTMLWidget - Like FromForm but with DBIx::Class and HTML::Widget.

DBIx::Class::HTMLWidget - FromFormと似ていますが、DBIx::Class とHTML::Widget。

DBIx::Class::Ordered - Modify the position of objects in an ordered list.

DBIx::Class::Ordered - 整列されたリスト内のオブジェクトの位置を修正する。

DBIx::Class::PK::Auto - Retrieve automatically created primary keys upon insert.

DBIx::Class::PK::Auto - insert で、自動的に作られたプライマリキーを取得する。

DBIx::Class::QueriesTime - Display the amount of time it takes to run queries.

DBIx::Class::QueriesTime - クエリを実行するのにかかった時間を表示する。

DBIx::Class::RandomStringColumns - Declare virtual columns that return random strings.

DBIx::Class::RandomStringColumns - ランダムな文字列を返す仮想カラムを宣言する。

DBIx::Class::UTF8Columns - Force UTF8 (Unicode) flag on columns.

DBIx::Class::UTF8Columns - カラムにUTF8 (Unicode) flagを強制する。

DBIx::Class::UUIDColumns - Implicit UUID columns.

DBIx::Class::UUIDColumns - Implicit UUID columns.

DBIx::Class::WebForm - CRUD methods.

DBIx::Class::WebForm - CRUD メソッド。

実験的

These components are under development, there interfaces may change, they may not work, etc. So, use them if you want, but be warned.

下記のコンポーネントは開発中です。インターフェースは代わるかもしれませんし、 動かなかったりするかもしれません。お望みなら使ってください、 ただし、注意してください。

DBIx::Class::Serialize - Hooks for Storable freeze/thaw.

DBIx::Class::Serialize - Storable freeze/thawのフック。

DBIx::Class::Serialize::Storable - Hooks for Storable freeze/thaw.

DBIx::Class::Serialize::Storable - Storable freeze/thawのフック。

DBIx::Class::Validation - Validate all data before submitting to your database.

DBIx::Class::Validation - データベースに入れる前に全てのデータが正しいか確認する。

コア

These are the components that all, or nearly all, people will use without even knowing it. These components provide most of DBIx::Class' functionality.

下記のコンポーネントは全ての、ほとんど全ての、人がそれを知らなくても使うでしょう。 下記のコンポーネントはDBIx::Classの機能のほとんどを提供します。

DBIx::Class::AccessorGroup - Lets you build groups of accessors.

DBIx::Class::AccessorGroup - アクセサのグルーを作ります。

DBIx::Class::Core - Loads various components that "most people" would want.

DBIx::Class::Core - "ほとんどの人"が欲しい様々なコンポーネントをロードします。

DBIx::Class::DB - Non-recommended classdata schema component.

DBIx::Class::DB - 非推奨のクラスデータスキーマコンポーネント。

DBIx::Class::InflateColumn - Automatically create objects from column data.

DBIx::Class::InflateColumn - カラムデータから自動的にオブジェクトを作る。

DBIx::Class::PK - This class contains methods for handling primary keys and methods depending on them.

DBIx::Class::PK - このクラスはプライマリキーを扱うメソッドとそれに依存するメソッドを含みます。

DBIx::Class::Relationship - Inter-table relationships.

DBIx::Class::Relationship - テーブル間のリレーションシップ。

DBIx::Class::ResultSourceProxy::Table - Provides a classdata table object and method proxies.

DBIx::Class::ResultSourceProxy::Table - クラスデータテーブルオブジェクトとメソッドプロキシを提供します。

DBIx::Class::Row - Basic row methods.

DBIx::Class::Row - 基本的な列のメソッド。

SEE ALSO

DBIx::Class::Manual::Cookbook

作者

Aran Clary Deltac <bluefeet@cpan.org>

翻訳について

翻訳者:加藤敦 (ktat.is at gmail.com)

Perlドキュメント日本語訳 Project にて、 Perlモジュール、ドキュメントの翻訳を行っております。

 http://perldocjp.sourceforge.jp/
 http://sourceforge.jp/projects/perldocjp/
 http://www.freeml.com/perldocjp/
 http://www.perldoc.jp