[SP-pm] DBIx::Class - problema com colunas
Daniel de Oliveira Mantovani
daniel.oliveira.mantovani at gmail.com
Thu Jun 17 06:06:26 PDT 2010
Olha o que eu encontrei no manual,
<manual>
Whole related objects
To fetch entire related objects, e.g. CDs and all Track data,
use the 'prefetch' attribute:
$schema->resultset('CD')->search(
{ 'Title' => 'Funky CD',
},
{ prefetch => 'tracks',
order_by => ['tracks.id'],
}
);
This will produce SQL similar to the following:
SELECT cd.ID, cd.Title, cd.Year, tracks.id, tracks.Name,
tracks.Artist FROM CD JOIN Tracks ON CD.ID = tracks.CDID WHERE
cd.Title = 'Funky CD' ORDER BY 'tracks.id';
The syntax of 'prefetch' is the same as 'join' and implies the
joining, so there is no need to use both together.
while(my $row = $search_rs->next) {
print $row->tracks->name; ## WORKS
}
</manual>
No seu vai ficar algo assim,
my @roll = $schema->resultset('spider_info')->search( {
prefetch => 'threads', })->all;
say $_->threads->thread_id
(Se o seu relacionamento estiver certo, será algo do gênero)
perldoc DBIx::Class::Manual::Joining
Até mais André!
[]'s
2010/6/17 Andre Carneiro <andregarciacarneiro em gmail.com>:
> Salve monges.
>
> Tenho no meu esquema duas classes que representam as tabelas, conforme
> abaixo:
> <tabela 1>
> package Scap2::DBIC::Schema::Spider_Info;
> use strict;
> use warnings;
> use base 'DBIx::Class';
> __PACKAGE__->load_components("Core");
> __PACKAGE__->table("spider_info");
> __PACKAGE__->add_columns(
> "spider_id",
> { data_type => "INTEGER", is_nullable => 0, size => undef },
> "spider_name",
> { data_type => "VARCHAR", is_nullable => 0, size => undef },
> "curr_process",
> { data_type => "INTEGER", is_nullable => 0, size => undef },
> "start_time",
> { data_type => "DATE", is_nullable => 0, size => undef },
> "end_time",
> { data_type => "DATE", is_nullable => 0, size => undef },
> "total_time",
> { data_type => "DATE", is_nullable => 0, size => undef },
> "parser_time",
> { data_type => "DATE", is_nullable => 0, size => undef },
> "amount",
> { data_type => "INTEGER", is_nullable => 0, size => undef },
> "amount_real",
> { data_type => "INTEGER", is_nullable => 0, size => undef },
> "status",
> { data_type => "INTEGER", is_nullable => 0, size => undef },
> "executed",
> { data_type => "INTEGER", is_nullable => 0, size => undef },
> );
> __PACKAGE__->set_primary_key("spider_name");
> #__PACKAGE__->has_many(name => "Scap2::DBIC::Schema::Job");
> __PACKAGE__->has_many(threads =>
> 'Scap2::DBIC::Schema::ThreadList','spider_name');
>
> </tabela 1>
>
> <tabela 2>
> package Scap2::DBIC::Schema::ThreadList;
> use strict;
> use warnings;
> use base 'DBIx::Class';
> __PACKAGE__->load_components("Core");
> __PACKAGE__->table("thread_list");
> __PACKAGE__->add_columns(
> "thread_id",
> { data_type => "integer", default_value => undef, is_nullable => 0, size
> => 20 },
> "spider_name",
> { data_type => "text", default_value => undef, is_nullable => 0, size =>
> 100 },
> "ospid",
> { data_type => "integer", default_value => undef, is_nullable => 0, size
> => 20 },
> "syspid",
> { data_type => "integer", default_value => undef, is_nullable => 0, size
> => 20 },
>
> );
> __PACKAGE__->set_primary_key('ospid');
> #__PACKAGE__->belongs_to(spider_info
> =>'Scap2::DBIC::Schema::Spider_Info','spider_name');
> __PACKAGE__->belongs_to(spider_info =>'Scap2::DBIC::Schema::Spider_Info' =>
> {'foreign.spider_name' => 'self.spider_name' } );
>
> </tabela2>
>
>
>
> Esses relacionamentos estão ok(não tá gerando erro pelo menos, e quando eu
> uso DBI_TRACE o join está correto!)
>
> A minha pesquisa utilizando essa relação, se resume no seguinte trecho de
> código:
>
> <pesquisa>
> sub load_all {
> my ( $self ) = @_;
> my @resultset = undef;
> # eval{@resultset =
> $self->schema->resultset('ThreadList')->related_resultset('spider_info')->all
> };
> # eval{@resultset =
> $self->schema->resultset('ThreadList')->search_related('spider_info',{});
> eval{@resultset = $self->schema->resultset('Spider_Info')->search({},{join
> => 'threads'});
> };
> if($@){
> print "\n\nOOOPS! Nao consegui montar a lista de threads --- $@\n\n"
> }else {
> my @list = ();
> $self->thread_list(\@resultset);
> }
> print Dumper $self->thread_list;
> return $self->thread_list;
> }
> </pesquisa>
>
> Minha pergunta é: Por que o DBIx::Class não inclui as colunas das duas
> tabelas envolvidas no relacionamento?
> SQL gerada pelo DBI_TRACE:
> SELECT me.spider_id, me.spider_name, me.curr_process, me.start_time,
> me.end_time, me.total_time, me.parser_time, me.amount, me.amount_real,
> me.status, me.executed FROM spider_info me LEFT JOIN thread_list threads ON
> threads.spider_name = me.spider_name
>
>
> Thx!
>
>
> --
> André Garcia Carneiro
> Analista/Desenvolvedor Perl
> (11)82907780
>
> _______________________________________________
> SaoPaulo-pm mailing list
> SaoPaulo-pm em pm.org
> http://mail.pm.org/mailman/listinfo/saopaulo-pm
>
--
"If you’ve never written anything thoughtful, then you’ve never had
any difficult, important, or interesting thoughts. That’s the secret:
people who don’t write, are people who don’t think."
More information about the SaoPaulo-pm
mailing list