0
DBIx::Class Primer How to do common day-to-day tasks.
Creating Rows: create() <ul><li>Get a resultset and call: $rs->create(vals). </li></ul><ul><li>Effectively a shortcut for:...
Updating Rows: update() $campaign-> update (   { campaign_name => ‘Test Test’ } ); # Is the same as... $campaign->campaign...
Deleting Rows # Delete all campaigns! $campaigns-> delete (); # Or just delete one row. $campaign-> delete ();
Find Rows: find() <ul><li>Returns a single row. </li></ul><ul><li>Argument is the value of the primary key. </li></ul><ul>...
find_or_create() my $campaign = $campaigns-> find_or_create (   { campaign_name => ‘Test Test’ } ); # Is the same as... my...
update_or_create() my $campaign = $campaigns-> update_or_create (   { campaign_name => ‘Test Test’, status => 0 } ); # Is ...
Relationships <ul><li>DBIC supports one-to-many, many-to-one, many-to-many, and one-to-one relationships. </li></ul><ul><l...
Resources <ul><li>DBIx::Class Manual @ CPAN http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/Manual.pod...
Upcoming SlideShare
Loading in...5
×

DBIC 3 - Primer

2,390

Published on

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.

Published in: Technology, News & Politics
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,390
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
55
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "DBIC 3 - Primer"

  1. 1. DBIx::Class Primer How to do common day-to-day tasks.
  2. 2. Creating Rows: create() <ul><li>Get a resultset and call: $rs->create(vals). </li></ul><ul><li>Effectively a shortcut for: $rs->new_result(vals)->insert(). </li></ul>my $campaign = $campaigns-> create ( { campaign_name => ‘Test’ } );
  3. 3. Updating Rows: update() $campaign-> update ( { campaign_name => ‘Test Test’ } ); # Is the same as... $campaign->campaign_name( ‘Test Test’ ); $campaign-> update ();
  4. 4. Deleting Rows # Delete all campaigns! $campaigns-> delete (); # Or just delete one row. $campaign-> delete ();
  5. 5. Find Rows: find() <ul><li>Returns a single row. </li></ul><ul><li>Argument is the value of the primary key. </li></ul><ul><li>Multiple values may be passed in the case of a multiple column primary key. </li></ul><ul><li>If passing multiple values then use named arguments. </li></ul>my $campaign = $campaigns-> find ( $cid ); my $translation = $translations-> find ( { message_code => $message_code, language_code => $language_code, accounting_division_id => 0, } );
  6. 6. 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’ } );
  7. 7. 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 } ); }
  8. 8. Relationships <ul><li>DBIC supports one-to-many, many-to-one, many-to-many, and one-to-one relationships. </li></ul><ul><li>Using these relationships is straightforward. </li></ul># 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();
  9. 9. Resources <ul><li>DBIx::Class Manual @ CPAN http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/Manual.pod </li></ul><ul><li>DBIx::Class Tutorial (under development) http://desert-island.me.uk:8888/perldoc/dbic-tutorial/lib/DBIx/Class/Tutorial/ </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×