


Rose::DB::ObjectU:s:eSrtdC(o3npt)ributed Perl DocuRmoesnet:a:tDiBo:n:Object::Std(3p)


NNAAMMEE
       Rose::DB::Object::Std - Standardized object representation of a single
       row in a database table.

SSYYNNOOPPSSIISS
         package Category;

         use base 'Rose::DB::Object::Std';

         __PACKAGE__->meta->setup
         (
           table => 'categories',

           columns =>
           [
             id          => { type => 'int', primary_key => 1 },
             name        => { type => 'varchar', length => 255 },
             description => { type => 'text' },
           ],

           unique_key => 'name',
         );

         ...

         package Product;

         use base 'Rose::DB::Object::Std';

         __PACKAGE__->meta->setup
         (
           table => 'products',

           columns =>
           [
             id          => { type => 'int', primary_key => 1 },
             name        => { type => 'varchar', length => 255 },
             description => { type => 'text' },
             category_id => { type => 'int' },

             status =>
             {
               type      => 'varchar',
               check_in  => [ 'active', 'inactive' ],
               default   => 'inactive',
             },

             start_date  => { type => 'datetime' },
             end_date    => { type => 'datetime' },

             date_created     => { type => 'timestamp', default => 'now' },
             last_modified    => { type => 'timestamp', default => 'now' },
           ],




perl v5.12.2                2009-07-07                          1





Rose::DB::ObjectU:s:eSrtdC(o3npt)ributed Perl DocuRmoesnet:a:tDiBo:n:Object::Std(3p)


           unique_key => 'name',

           foreign_keys =>
           [
             category =>
             {
               class       => 'Category',
               key_columns => { category_id => 'id' },
             },
           ],
         );

         ...

         $product = Product->new(name        => 'GameCube',
                                 status      => 'active',
                                 start_date  => '11/5/2001',
                                 end_date    => '12/1/2007',
                                 category_id => 5);

         $product->save or die $product->error;

         $id = $product->id; # auto-generated on save

         ...

         $product = Product->new(id => $id);
         $product->load or die $product->error;

         print $product->category->name;

         $product->end_date->add(days => 45);

         $product->save or die $product->error;

         ...

DDEESSCCRRIIPPTTIIOONN
       Rose::DB::Object::Std is a subclass of Rose::DB::Object that imposes a
       few more constraints on the tables it fronts.  In addition to the
       constraints described in the Rose::DB::Object documentation, tables
       fronted by Rose::DB::Object::Std objects must also fulfill the
       following requirements:

       +o   The table must have a single primary key column named "id"

       +o   The value of the "id" column must be auto-generated if absent.

       Different databases provide for auto-generated column values in
       different ways.  Some provide a native "auto-increment" or "serial"
       data type, others use sequences behind the scenes.

       Rose::DB::Object::Std (in cooperation with Rose::DB and
       Rose::DB::Object::Std::Metadata) attempts to hide these details from



perl v5.12.2                2009-07-07                          2





Rose::DB::ObjectU:s:eSrtdC(o3npt)ributed Perl DocuRmoesnet:a:tDiBo:n:Object::Std(3p)


       you.  All you have to do is omit the value for the primary key
       entirely.  After the object is "save()"ed, you can retrieve the auto-
       selected primary key by calling the "id()" method.

       You do have to correctly define the "id" column in the database,
       however.  Here are examples of primary key column definitions that
       provide auto-generated values, one for each of the databases supported
       by Rose::DB.

       +o   PostgreSQL

               CREATE TABLE mytable
               (
                 id   SERIAL NOT NULL PRIMARY KEY,
                 ...
               );

       +o   MySQL

               CREATE TABLE mytable
               (
                 id   INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                 ...
               );

       +o   Informix

               CREATE TABLE mytable
               (
                 id   SERIAL NOT NULL PRIMARY KEY,
                 ...
               );

       Other data definitions are possible, of course, but the three
       definitions above are used in the Rose::DB::Object::Std test suite and
       are therefore guaranteed to work.  If you have success with alternative
       approaches, patches and/or new tests are welcome.

       To achieve much of this functionality, Rose::DB::Object::Std uses
       Rose::DB::Object::Std::Metadata objects.  The "meta()" method will
       create these form you.  You should not need to do anything special if
       you use the idimoatic approach to defining metadata as shown in the
       synopsis.

MMEETTHHOODDSS
       Only the methods that are overridden are documented here.  See the
       Rose::DB::Object documentation for the rest.

       mmeettaa
           Returns the Rose::DB::Object::Std::Metadata object associated with
           this class.  This object describes the database table whose rows
           are fronted by this class: the name of the table, its columns,
           unique keys, foreign keys, etc.  See the
           Rose::DB::Object::Std::Metadata documentation for more information.



perl v5.12.2                2009-07-07                          3





Rose::DB::ObjectU:s:eSrtdC(o3npt)ributed Perl DocuRmoesnet:a:tDiBo:n:Object::Std(3p)


           This can be used as both a class method and an object method.

AAUUTTHHOORR
       John C. Siracusa (siracusa@gmail.com)

LLIICCEENNSSEE
       Copyright (c) 2009 by John C. Siracusa.  All rights reserved.  This
       program is free software; you can redistribute it and/or modify it
       under the same terms as Perl itself.
















































perl v5.12.2                2009-07-07                          4


