DBIC 3 - Primer
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

DBIC 3 - Primer

on

  • 3,615 views

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.

Statistics

Views

Total Views
3,615
Views on SlideShare
3,598
Embed Views
17

Actions

Likes
2
Downloads
54
Comments
0

1 Embed 17

http://www.slideshare.net 17

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

DBIC 3 - Primer Presentation Transcript

  • 1. DBIx::Class Primer How to do common day-to-day tasks.
  • 2. 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’ } );
  • 3. Updating Rows: update() $campaign-> update ( { campaign_name => ‘Test Test’ } ); # Is the same as... $campaign->campaign_name( ‘Test Test’ ); $campaign-> update ();
  • 4. Deleting Rows # Delete all campaigns! $campaigns-> delete (); # Or just delete one row. $campaign-> delete ();
  • 5. 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, } );
  • 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. 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. 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();
  • 9. Resources
    • DBIx::Class Manual @ CPAN http://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/