dbic 3 - primer
DESCRIPTION
Gives an introduction to working with DBIx::Class row objects as well as other mid-level DBIC concepts. Assumes an understanding of databases and a rudimentary understanding of DBIC.TRANSCRIPT
![Page 1: DBIC 3 - Primer](https://reader036.vdocuments.mx/reader036/viewer/2022081809/54b44fdf4a79598e018b45db/html5/thumbnails/1.jpg)
DBIx::Class Primer
How to do common day-to-day tasks.
![Page 2: DBIC 3 - Primer](https://reader036.vdocuments.mx/reader036/viewer/2022081809/54b44fdf4a79598e018b45db/html5/thumbnails/2.jpg)
Creating Rows: create()
• Get a resultset and call:$rs->create(\%vals).
• Effectively a shortcut for:$rs->new_result(\%vals)->insert().
my $campaign = $campaigns->create( { campaign_name => ‘Test’ });
![Page 3: DBIC 3 - Primer](https://reader036.vdocuments.mx/reader036/viewer/2022081809/54b44fdf4a79598e018b45db/html5/thumbnails/3.jpg)
Updating Rows: update()
$campaign->update( { campaign_name => ‘Test Test’ });
# Is the same as...
$campaign->campaign_name( ‘Test Test’ );$campaign->update();
![Page 4: DBIC 3 - Primer](https://reader036.vdocuments.mx/reader036/viewer/2022081809/54b44fdf4a79598e018b45db/html5/thumbnails/4.jpg)
Deleting Rows
# Delete all campaigns!
$campaigns->delete();
# Or just delete one row.
$campaign->delete();
![Page 5: DBIC 3 - Primer](https://reader036.vdocuments.mx/reader036/viewer/2022081809/54b44fdf4a79598e018b45db/html5/thumbnails/5.jpg)
Find Rows: find()
• Returns a single row.• Argument is the value of the primary key.• Multiple values may be passed in the case of a
multiple column primary key.• If passing multiple values then use named
arguments.
my $campaign = $campaigns->find( $cid );
my $translation = $translations->find( { message_code => $message_code, language_code => $language_code, accounting_division_id => 0,} );
![Page 6: DBIC 3 - Primer](https://reader036.vdocuments.mx/reader036/viewer/2022081809/54b44fdf4a79598e018b45db/html5/thumbnails/6.jpg)
find_or_create()
my $campaign = $campaigns->find_or_create( { campaign_name => ‘Test Test’ });
# Is the same as...
my $campaign = $campaigns->find( { campaign_name => ‘Test Test’ });$campaign ||= $campaigns->create( { campaign_name => ‘Test Test’ });
![Page 7: DBIC 3 - Primer](https://reader036.vdocuments.mx/reader036/viewer/2022081809/54b44fdf4a79598e018b45db/html5/thumbnails/7.jpg)
update_or_create()
my $campaign = $campaigns->update_or_create( { campaign_name => ‘Test Test’, status => 0 });
# Is the same as...
my $campaign = $campaigns->find( { campaign_name => ‘Test Test’ });
if ($campaign) { $campaign->update({ status=>0 });} else { $campaign = $campaigns->create( { campaign_name=>‘Test Test’, status=>0 } );}
![Page 8: DBIC 3 - Primer](https://reader036.vdocuments.mx/reader036/viewer/2022081809/54b44fdf4a79598e018b45db/html5/thumbnails/8.jpg)
Relationships
• DBIC supports one-to-many, many-to-one, many-to-many, and one-to-one relationships.
• Using these relationships is straightforward.
# A one-to-many (has_many) relationship:my $campaigns = $advertiser->campaigns();
# A many-to-one (belongs_to) relationship:my $advertiser = $campaign->advertiser();
# A one-to-one (has_one) relationship:my $money = $campaign->money();
# A many-to-many (many_to_many) relationship:my $users = $role->users();
![Page 9: DBIC 3 - Primer](https://reader036.vdocuments.mx/reader036/viewer/2022081809/54b44fdf4a79598e018b45db/html5/thumbnails/9.jpg)
Resources
• DBIx::Class Manual @ CPANhttp://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/Manual.pod
• DBIx::Class Tutorial (under development)http://desert-island.me.uk:8888/perldoc/dbic-tutorial/lib/DBIx/Class/Tutorial/