[Dbix-class] Shortcut to perform lookups

Jason Galea lists at eightdegrees.com.au
Tue Jul 7 01:17:26 GMT 2009


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@lists.scsys.co.uk
>>
>> _______________________________________________
>> 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@lists.scsys.co.uk
>
>
> _______________________________________________
> 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@lists.scsys.co.uk



More information about the DBIx-Class mailing list