[Dbix-class] Passing values to a custom join condition
Andrew Beverley
andy at andybev.com
Sun Jan 19 17:10:29 GMT 2014
On Tue, 2014-01-07 at 00:24 +0000, Lianna Eeftinck wrote:
> I would do something like this, assuming you're looking for a left join:
>
> Result/Task.pm:
> __PACKAGE__->has_many(
> "site_tasks", "Lenio::Schema::Result::SiteTask",
> { 'foreign.task_id' => 'self.id' },
> { join_type => 'left', cascade_copy => 0, cascade_delete => 0 },
> );
>
> ResultSet/SiteTask.pm:
> sub by_site_id {
> my ( $self, $site_id ) = @_;
>
> my $me = $self->current_source_alias;
> return $self->search_rs(
> { "$me.site_id" => $site_id },
> );
> }
>
> Then you can call this as:
> my $site_task_rs = $schema->resultset('Task')->site_tasks->by_site_id(
> 101 );
>
> This should do the same as:
> my $site_task_rs = $schema->resultset('Task')->search(
> { 'site_task.site_id' => 101 },
> { join => 'site_task' }, # or prefetch rather than join
> );
Thanks for the reply. I've not tried it, but unfortunately I don't think
that would work. The problem is that I still want to retrieve the items
from "task", but if the equivalent entry doesn't appear in site_task,
for the value from the join to be a null. If I use any sort of WHERE
condition, then the "task" values will be missing.
Thanks,
Andy
More information about the DBIx-Class
mailing list