DBIC折腾了半天,也算学了点东西

上一篇 / 下一篇  2008-03-17 00:16:13 / 个人分类:perl

终于捉住了点门道。
首先是这个belongs_to,当主表report_group的多列主键的时候,第三个参数就必须是条件来制定连接了,也是符合实际意义的吧,否则不就会有无意义数据么。当定义条件的时候foreign和self算是类似两个保留字的作用。

#base_mach.pm
__PACKAGE__->belongs_to(mach_group => 'CBSDB::Report_group',{'foreign.app'=>'self.app','foreign.cat'=>'sel
f.cat'});

另外也算学会了DBIC 两表连接的sql书写。
my $base_mach = [$schema->resultset('Base_mach')->search({'me.app' => 'Penguin'},{join=>'mach_group',prefe
tch=>'mach_group'})];

上边的join参数一定的记住那是定义的关系名,其实也就是table pm里边belongs_to或者has_many的第一个参数,belongs_to,has_many是定义关系的。

resultset的find方法返回的仅仅是一个row object

find

Arguments: @values | \%cols, \%attrs?
Return Value: $row_object

Finds a row based on its primary key or unique constraint. For example, to find a row by its primary key:

my $cd = $schema->resultset('CD')->find(5);

而resetsult返回是:标量环境是一个resultset,list环境就是row obj数组了

search

Arguments: $cond, \%attrs?
Return Value: $resultset (scalar context), @row_objs (list context)
my @cds = $cd_rs->search({ year => 2001 }); # "... WHERE year = 2001" my $new_rs = $cd_rs->search({ year => 2005 }); my $new_rs = $cd_rs->search([ { year => 2005 }, { year => 2004 } ]); # year = 2005 OR year = 2004

DBIC这个东西怎么说呢,强大,繁琐,基本上不会影响sql的表达吧
很复杂的sql可以参考DBIC cookbook,cookbook还是不错的,应该把一般问题都覆盖到了

TAG: perl

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-09-05  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 3761
  • 日志数: 726
  • 建立时间: 2008-01-14
  • 更新时间: 2008-09-01

RSS订阅

Open Toolbar