Shortcut to perform lookups

8 messages Options
Embed this post
Permalink
Dan Horne

Shortcut to perform lookups

Reply Threaded More More options
Print post
Permalink
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
})

I've had a peruse of the Cookbook, but couldn't see anything

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
David Ihnen

Re: Shortcut to perform lookups

Reply Threaded More More options
Print post
Permalink
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.

David


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Dan Horne

Re: Shortcut to perform lookups

Reply Threaded More More options
Print post
Permalink

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),
});

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Jason Galea

Re: Shortcut to perform lookups

Reply Threaded More More options
Print post
Permalink
In reply to this post by Dan Horne
The docs appear to indicate that you can't do this when the related
foreign row already exists, but it seems to work for me..

my $customer = $self->schema->resultset('customer')->create({
   name => 'Acme Corp',
   status_id => { name => 'new'},
});

cheers,

J


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
> })
>
> I've had a peruse of the Cookbook, but couldn't see anything
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@...

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Dan Horne

Re: Shortcut to perform lookups

Reply Threaded More More options
Print post
Permalink
Hey this is cool. Which doc is this in? If its not currently supported  
for existing records, it'd be great if it became standard - I'd hate  
to rely on something that was dropped on a future version of DBIC  
because it wasn't official.
On 7/07/2009, at 01:02 PM, Jason Galea wrote:

> The docs appear to indicate that you can't do this when the related  
> foreign row already exists, but it seems to work for me..
>
> my $customer = $self->schema->resultset('customer')->create({
>  name => 'Acme Corp',
>  status_id => { name => 'new'},
> });
>
> cheers,
>
> J
>
>
> 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
>> })
>>
>> I've had a peruse of the Cookbook, but couldn't see anything
>>
>> _______________________________________________
>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>> IRC: irc.perl.org#dbix-class
>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>> Searchable Archive: http://www.grokbase.com/group/dbix-class@...
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@...


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Jason Galea

Re: Shortcut to perform lookups

Reply Threaded More More options
Print post
Permalink

http://search.cpan.org/~ribasushi/DBIx-Class-0.08107/lib/DBIx/Class/ResultSet.pm#create

Attempt to create a single new row or a row with multiple related rows
in the table represented by the resultset (and related tables). This
will not check for duplicate rows before inserting, use "find_or_create"
<http://search.cpan.org/%7Eribasushi/DBIx-Class-0.08107/lib/DBIx/Class/ResultSet.pm#find_or_create>
to do that.

*To create one row for this resultset, pass a hashref of key/value pairs
representing the columns of the table and the values you wish to store.
If the appropriate relationships are set up, foreign key fields can also
be passed an object representing the foreign row, and the value will be
set to its primary key.*

To create related objects, pass a hashref for the value if the related
item is a foreign key relationship ("belongs_to" in
DBIx::Class::Relationship
<http://search.cpan.org/%7Eribasushi/DBIx-Class-0.08107/lib/DBIx/Class/Relationship.pm#belongs_to>),
and use the name of the relationship as the key. (NOT the name of the
field, necessarily). For |has_many| and |has_one| relationships, pass an
arrayref of hashrefs containing the data for each of the rows to create
in the foreign tables, again using the relationship name as the key.

Instead of hashrefs of plain related data (key/value pairs), you may
also pass new or inserted objects. New objects (not inserted yet, see
"new"
<http://search.cpan.org/%7Eribasushi/DBIx-Class-0.08107/lib/DBIx/Class/ResultSet.pm#new>),
will be inserted into their appropriate tables.


Dan Horne wrote:

> Hey this is cool. Which doc is this in? If its not currently supported
> for existing records, it'd be great if it became standard - I'd hate
> to rely on something that was dropped on a future version of DBIC
> because it wasn't official.
> On 7/07/2009, at 01:02 PM, Jason Galea wrote:
>
>> The docs appear to indicate that you can't do this when the related
>> foreign row already exists, but it seems to work for me..
>>
>> my $customer = $self->schema->resultset('customer')->create({
>>  name => 'Acme Corp',
>>  status_id => { name => 'new'},
>> });
>>
>> cheers,
>>
>> J
>>
>>
>> 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
>>> })
>>>
>>> I've had a peruse of the Cookbook, but couldn't see anything
>>>
>>> _______________________________________________
>>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>>> IRC: irc.perl.org#dbix-class
>>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>>> Searchable Archive:
>>> http://www.grokbase.com/group/dbix-class@...
>>
>> _______________________________________________
>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>> IRC: irc.perl.org#dbix-class
>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>> Searchable Archive:
>> http://www.grokbase.com/group/dbix-class@...
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@...

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
David Ihnen

Re: Shortcut to perform lookups

Reply Threaded More More options
Print post
Permalink
In reply to this post by Dan Horne
Dan Horne wrote:

>
> 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),
> });

I usually end up doing something vaguely like that, actually.  Only I'd
name my rels status, phase, and section and do something like this...

I'll create 'create_process' method on the table class for the section -
giving a pre-existing context for the creation of a process.  The status
and phase of course will have to be passed in - but I like to handle
them as objects instead of arbitrary id numbers, instantiated as early
as possible in the request instead of trying to query for the id
explicitly.  There's alot more you can do with an object than just an
ID.  And put the modification sequence in a transaction.

sub create_process {
  my ($self, $status_obj, $phase_obj) = @_;
  $self->txn_do( sub { $self->create({ status => $status_obj, phase =>
$phase_obj }) } );
}

I don't know of any shortcuts beyond providing context for such
operations, in this case this could be called a factory pattern where
create_process is a factory for ETLProcess objects related to a
particular section object.  The creation of a ETLProcess object requires
the existance of status, phase, and section objects as part of it, so
you're going to have to pull that off somehow.

David




_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
David Ihnen

Re: Shortcut to perform lookups

Reply Threaded More More options
Print post
Permalink

Oye, at least when you're coding you don't make silly ommissions... like
not doing create_related.
>
>  my ($self, $status_obj, $phase_obj) = @_;
>  $self->txn_do( sub { $self->create_related('process', { status =>
> $status_obj, phase => $phase_obj }) } );
> }


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...