@kyanny's blog

My thoughts, my life. Views/opinions are my own.

Catalyst のヘルパースクリプトの使い方がわからない

script/myapp_create.pl の使い方がよくわからない・・・。 model が特に。 DBIx::Class::Schema::Loader を使ったことがなくて、それの使い方がわかってないから、さらに混乱に追い討ちがかかっているなぁ。

Catalyst::Manual::Tutorial の該当個所を読んでみたけど、やっぱりよくわからないなぁ。最初に lib/MyAppDB.pm を作らないとダメってことなのか?それをしなくていいために Schema::Loader ってのがあるんじゃないの?

Use Catalyst::Model::DBIC::Schema To Load The Model Class

Catalst::Model::DBIC::Schema をモデルクラスをロードするために使う

When Catalyst::Model::DBIC::Schema is in use, Catalyst essentially reads an existing copy of your database model and creates a new set of objects under MyApp::Model for use inside of Catalyst.

Catalyst::Model::DBIC::Schema を使うと、 Catalyst はあなたのデータベースモデルのコピーから読み込み、 Catalyst 内から使えるように MyApp::Model に新しいオブジェクトを作ります。

Note: With Catalyst::Model::DBIC::Schema you essentially end up with two sets of model classes (only one of which you write... the other set is created automatically in memory when your Catalyst application initializes). For this tutorial application, the important points to remember are: you write the result source files in MyAppDB, but within Catalyst you use the automatically created model classes in MyApp::Model.

注意: あなたは Catalyst::Model::DBIC::Schema を二つのモデルクラスとして使うことができます (一つは先ほどあなたが書いたもの、もう一つは Catalyst アプリケーションの初期化時に自動的にメモリ上に作られます) このチュートリアルアプリケーションで覚えておきたい大切なことは、あなたは MyAppDB に result source ファイルをかきます、しかし Catalyst は MyApp::Model にモデルクラスを自動的に作ります。

Use the Catalyst::Helper::Model::DBIC::Schema helper script to create the model class that loads up the model we created in the previous step:

先ほどつくったモデルからモデルクラスを読み込むために Catalyst::Helper::Model::DBIC::Schema ヘルパースクリプトを使います。

    $ script/myapp_create.pl model MyAppDB DBIC::Schema MyAppDB dbi:SQLite:myapp.db '' '' '{ AutoCommit => 1 }'
     exists "/root/dev/MyApp/script/../lib/MyApp/Model"
     exists "/root/dev/MyApp/script/../t"
    created "/root/dev/MyApp/script/../lib/MyApp/Model/MyAppDB.pm"
    created "/root/dev/MyApp/script/../t/model_MyAppDB.t"

Where the first MyAppDB is the name of the class to be created by the helper in lib/MyApp/Model and the second MyAppDB is the name of existing schema file we created (in lib/MyAppDB.pm). You can see that the helper creates a model file under lib/MyApp/Model (Catalyst has a separate directory under lib/MyApp for each of the three parts of MVC: Model, View, and Controller [although older Catalyst applications often use the directories M, V, and C]).

最初の MyAppDB という名前はヘルパースクリプトによって lib/MyApp/Model に作られるモデルクラスの名前で、二つめの MyAppDB という名前は lib/MyAppDB.pm に先ほど作ったスキーマクラスの名前です。ヘルパーが lib/MyApp/Model 以下にモデルファイルを作るのがみえるでしょう (Catalyst は lib/MyApp の下に三つの分離された MVC: Model View Controller ディレクトリを持っています)

適当に訳してみた。やっぱり、先に MyAppDB.pm を作っておかないとダメということか・・・。

で、作ってみて helper はちゃんと動くんだけど、 controller から $c->model("DBIC::Foo")->find(1); などとすると server.pl が起動できなくなる。。

DBIC::Foo ではダメで (Model::DBIC とかはコントローラーから呼べないものなのか??) model("MyAppDB::Foo")->find(1) などとする。

lib/MyAppDB/Foo.pm があるだけではダメで lib/MyAppDB.pm も必要。

せっかく Model ってディレクトリがあるのに、他にわざわざスキーマを書いてやる必要があるっていうのは直感的じゃない気がするなぁ・・・。なんのために Model ってディレクトリがあるのかがわからない。