DBICメモ

面倒なので、メモ
CPAN DBIx-Class http://search.cpan.org/~mstrout/DBIx-Class/
超くわしい DBIC http://nekokak.jf.land.to/wiki/wiki.cgi/sub?page=Perl%2FDBIC

find(id => $id)

findはプライマリーキーを探すとき。
findでプライマリーキー以外で調べてもデータとれるけど、けいこくでちゃいます。

search( name => $name)

my $ite = ***->search( name => $name,category => 'book',{order_by => 'hoge DESC, moge'});
my $itemname = $ite->first->name;
とかで検索結果のnameをげっと
nextとかで次のデータとか使える超便利

GROUP BY

->search_literal($jouken,@value, {group_by =>'category'});

これでcategoryでGROUP BY

DISTINCT

DISTINCT
distinct
Value: (0 | 1)
Set to 1 to group by all columns.

リレーション

下準備

MyApp/Schema.pm
__PACKAGE__->load_classes(qw/Table1 Table2/);


MyApp/Schema/Table1.pm
__PACKAGE__->belongs_to(user_id => 'MyApp::Schema::Table2');

MyApp/Schema/Table2.pm
__PACKAGE__->has_many(table1 => 'MyApp::Schema::Table1','user_id');


Table1とTable2は1対多

1(Table1:user_id) 対 多(Table2:user_id)

my @table2 = $c->model('MyApp::Table2')->search(undef);
if(@table2){
  foreach my $table2data (@table2){
      my $table1data = $table2data->user_id;
      print $table1data->username;
  }
}

こんなかんじでデータがとれる

リレーションのorder_by

は普通に結果に->search(undef,{order_by => 'hoge DESC'});
でソートしてくれる。

http://search.cpan.org/~danieltwc/DBIx-Class-0.07002/lib/DBIx/Class/ResultSet.pm#search