简体   繁体   中英

Joining search results of a table that “belongs_to” another

visits have many visitsTasks which belong to tasks .

I am looking at a particular row ( visitID ) in the visits table, and want to find all the tasks that were completed. Of those tasks, I want to access the path and mode columns (in tasks table).

      my $visitTasks = $visit->visits_tasks()->search(
            {'me.subsection' => 'completed'},
            {join => 'task_name' } # PROBLEM HERE
            ); 
      while(my $vt=$visitTasks->next()){
         say Dumper($vt->get_columns());

         # if completed is true then record the task
         push @{$visit{Tasks}},  {taskName => $vt->task_name, path=>$vt->path, mode=>$vt->mode } if $vt->value == 1;
      }

taskName and value ( visits_tasks columns) are available. But I'm unable to find the syntax or the missing relations to get the visitsTasks results joined with tasks


I created a UML diagram with dia , transformed the graph into mySQL with parsediasql , and populated the DBIX::class with dbicdump .

UML图

visitsTasks

package lncddb3::Schema::Result::VisitsTask;
#...
=head2 task_name

Type: belongs_to

Related object: L<lncddb3::Schema::Result::Task>

=cut

__PACKAGE__->belongs_to(
  "task_name",
  "lncddb3::Schema::Result::Task",
  { taskName => "taskName" },
  {
    is_deferrable => 1,
    join_type     => "LEFT",
    on_delete     => "RESTRICT",
    on_update     => "RESTRICT",
  },  
);

Tasks

package lncddb3::Schema::Result::Task;
# ...
=head2 visits_tasks

Type: has_many

Related object: L<lncddb3::Schema::Result::VisitsTask>

=cut

__PACKAGE__->has_many(
  "visits_tasks",
  "lncddb3::Schema::Result::VisitsTask",
  { "foreign.taskName" => "self.taskName" },
  { cascade_copy => 0, cascade_delete => 0 },
);

schema

create table visits (
   visitID      int unsigned    not null auto_increment          ,
   peopleID     int unsigned                      ,
   visitdate    date                              ,
   visitTime    time                              ,
   scanID       varchar(50)                       ,
   noShow       bool                              ,
   schedualedBy varchar(50)                       ,
   checkedInBy  varchar(50)                       ,
   googleid     varchar(50)                       ,
   age          double unsigned                   ,
   onMeds       varchar(20)                       ,
   location     varchar(50)                       ,
   cohort       varchar(50)      default "control",
   quality      int                               ,
   constraint pk_visits primary key (visitID)
) 
create table visitsTasks (
   peopleID   int unsigned ,
   visitID    int unsigned ,
   taskName   varchar(50)  ,
   subsection varchar(50)  ,
   value      varchar(50)
) 
create table tasks (
   taskName varchar(50)  not null,
   taskDesc char(200)            ,
   path     varchar(300)         ,
   mode     varchar(50)          ,--  fMRI, PET, MEG,fMRI+eyetracking,eyetracking, etc
   constraint pk_tasks primary key (taskName)
)

Needed to spend more time with the Cookbook . The joined columns are not at the same level as the original columns.

$vt->task_name->path #not $vt->path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM