[Dbix-class] Shortcut to perform lookups

Dan Horne dan.horne at redbone.co.nz
Tue Jul 7 01:00:49 GMT 2009


On 7/07/2009, at 12:43 PM, David Ihnen wrote:

> Dan Horne wrote:
>> Say I have a table called customer  which links to a parent table  
>> called status. I wonder if there is a shortcut so that I don't need  
>> to figure out the parent id in advance if I know the unique name.
>>
>> my $status = $self->schema->resultset('Status')- 
>> >search({status_name => 'new'})->single;
>>
>> my $customer = $self->schema->resultset->create({
>>    name => 'Acme Corp',
>>    status_id => $status->id
>> })
>>
>
> Like this?
>
> $self->schema->resultset('Status')->search({status_name => 'new'})- 
> >single->create_related( 'customer', { name => 'Acme Corp' } );
>
> You didn't put resultset('customer') in your second statement, btw.

Whoops - I was trying to contrive an example without including all the  
cruft of my code, which can lead to there kind of errors.

Thanks for your response. It works well for one parent table. What I'm  
really hoping to do is avoid writing code like this


my $process = $self->schema->resultset('ETLProcess')->create({
    name => $name,
    status_id => $self->get_status_id($status_name),
    phase_id => $self->get_phase_id($phase_name),
    section_id => $self->get_section_id($section_name),
});



More information about the DBIx-Class mailing list