Your SlideShare is downloading. ×
0
DBIC 3 - Primer
DBIC 3 - Primer
DBIC 3 - Primer
DBIC 3 - Primer
DBIC 3 - Primer
DBIC 3 - Primer
DBIC 3 - Primer
DBIC 3 - Primer
DBIC 3 - Primer
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

DBIC 3 - Primer

2,383

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.

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,383
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
55
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. DBIx::Class Primer How to do common day-to-day tasks.
  • 2. Creating Rows: create() <ul><li>Get a resultset and call: $rs-&gt;create(vals). </li></ul><ul><li>Effectively a shortcut for: $rs-&gt;new_result(vals)-&gt;insert(). </li></ul>my $campaign = $campaigns-&gt; create ( { campaign_name =&gt; ‘Test’ } );
  • 3. Updating Rows: update() $campaign-&gt; update ( { campaign_name =&gt; ‘Test Test’ } ); # Is the same as... $campaign-&gt;campaign_name( ‘Test Test’ ); $campaign-&gt; update ();
  • 4. Deleting Rows # Delete all campaigns! $campaigns-&gt; delete (); # Or just delete one row. $campaign-&gt; delete ();
  • 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-&gt; find ( $cid ); my $translation = $translations-&gt; find ( { message_code =&gt; $message_code, language_code =&gt; $language_code, accounting_division_id =&gt; 0, } );
  • 6. find_or_create() my $campaign = $campaigns-&gt; find_or_create ( { campaign_name =&gt; ‘Test Test’ } ); # Is the same as... my $campaign = $campaigns-&gt; find ( { campaign_name =&gt; ‘Test Test’ } ); $campaign ||= $campaigns-&gt; create ( { campaign_name =&gt; ‘Test Test’ } );
  • 7. update_or_create() my $campaign = $campaigns-&gt; update_or_create ( { campaign_name =&gt; ‘Test Test’, status =&gt; 0 } ); # Is the same as... my $campaign = $campaigns-&gt; find ( { campaign_name =&gt; ‘Test Test’ } ); if ($campaign) { $campaign-&gt; update ({ status=&gt;0 }); } else { $campaign = $campaigns-&gt; create ( { campaign_name=&gt;‘Test Test’, status=&gt;0 } ); }
  • 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-&gt;campaigns(); # A many-to-one (belongs_to) relationship: my $advertiser = $campaign-&gt;advertiser(); # A one-to-one (has_one) relationship: my $money = $campaign-&gt;money(); # A many-to-many (many_to_many) relationship: my $users = $role-&gt;users();
  • 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>

×