Rose::DB::Object::Manager

Rose::DB::Object::Manager module can fetch multiple Rose::DB::Object-derived objects from the database using complex queries.
Download

Rose::DB::Object::Manager Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • John C. Siracusa
  • Publisher web site:
  • http://search.cpan.org/~jsiracusa/

Rose::DB::Object::Manager Tags


Rose::DB::Object::Manager Description

Rose::DB::Object::Manager module can fetch multiple Rose::DB::Object-derived objects from the database using complex queries. Rose::DB::Object::Manager module can fetch multiple Rose::DB::Object-derived objects from the database using complex queries.SYNOPSIS ## ## Given the following Rose::DB::Object-derived classes... ## package Category; use base 'Rose::DB::Object'; __PACKAGE__->meta->setup ( table => 'categories', columns => , unique_key => 'name', ); ... package CodeName; use base 'Rose::DB::Object'; __PACKAGE__->meta->setup ( table => 'code_names', columns => , foreign_keys => , ); ... package Product; use base 'Rose::DB::Object'; __PACKAGE__->meta->setup ( table => 'products', columns => , default => 'inactive', }, start_date => { type => 'datetime' }, end_date => { type => 'datetime' }, date_created => { type => 'timestamp', default => 'now' }, last_modified => { type => 'timestamp', default => 'now' }, ], unique_key => 'name', foreign_keys => , relationships => , ); ... ## ## Create a manager class ## package Product::Manager; use base 'Rose::DB::Object::Manager'; sub object_class { 'Product' } __PACKAGE__->make_manager_methods('products'); # The call above creates the methods shown below. (The actual # method bodies vary slightly, but this is the gist of it...) # # sub get_products # { # shift->get_objects(@_, object_class => 'Product'); # } # # sub get_products_iterator # { # shift->get_objects_iterator(@_, object_class => 'Product'); # } # # sub get_products_count # { # shift->get_objects_count(@_, object_class => 'Product'); # } # # sub update_products # { # shift->update_objects(@_, object_class => 'Product'); # } # # sub delete_products # { # shift->delete_objects(@_, object_class => 'Product'); # } ... ## ## Use the manager class ## # # Get a reference to an array of objects # $products = Product::Manager->get_products ( query => , status => 'active', start_date => { lt => '15/12/2005 6:30 p.m.' }, name => { like => }, ], sort_by => 'category_id, start_date DESC', limit => 100, offset => 80, ); foreach my $product (@$products) { print $product->id, ' ', $product->name, "n"; } # # Get objects iterator # $iterator = Product::Manager->get_products_iterator ( query => , status => 'active', start_date => { lt => '15/12/2005 6:30 p.m.' }, name => { like => }, ], sort_by => 'category_id, start_date DESC', limit => 100, offset => 80, ); while($product = $iterator->next) { print $product->id, ' ', $product->name, "n"; } print $iterator->total; # # Get objects count # $count = Product::Manager->get_products_count ( query => , status => 'active', start_date => { lt => '15/12/2005 6:30 p.m.' }, name => { like => }, ], ); die Product::Manager->error unless(defined $count); print $count; # or Product::Manager->total() # # Get objects and sub-objects in a single query # $products = Product::Manager->get_products ( with_objects => , query => , status => 'active', start_date => { lt => '15/12/2005 6:30 p.m.' }, # We need to disambiguate the "name" column below since it # appears in more than one table referenced by this query. # When more than one table is queried, the tables have numbered # aliases starting from the "main" table ("products"). The # "products" table is t1, "categories" is t2, and "code_names" # is t3. You can read more about automatic table aliasing in # the documentation for the get_objects() method below. # # "category.name" and "categories.name" would work too, since # table and relationship names are also valid prefixes. 't2.name' => { like => }, ], sort_by => 'category_id, start_date DESC', limit => 100, offset => 80, ); foreach my $product (@$products) { # The call to $product->category does not hit the database print $product->name, ': ', $product->category->name, "n"; # The call to $product->code_names does not hit the database foreach my $code_name ($product->code_names) { # This call doesn't hit the database either print $code_name->name, "n"; } } # # Update objects # $num_rows_updated = Product::Manager->update_products( set => { end_date => DateTime->now, region_num => { sql => 'region_num * -1' } status => 'defunct', }, where => , ]); # # Delete objects # $num_rows_deleted = Product::Manager->delete_products( where => , name => { like => 'Wax%' } or => , ]); Requirements: · Perl


Rose::DB::Object::Manager Related Software