On Jan 16, 2008 11:40 AM, Bianka Martinovic <[EMAIL PROTECTED]> wrote:
> my $row     = $schema->new(
>                       db   => $dbh,
>                       %{
>                           $self->_prepare_data( $schema, { $self-
> >tableset( 'idfield' ) => $attrs{'dn'} } )
>                       }
>                   )->load;
> $row->init( %$data );
>
> (Where _prepare_data is the function to correctly map column names to
> hash keys and tableset() is a "driver function" that provides access to
> some config data for the current table. Please remember, this is a
> generic application which does not only synchronize databases.)
>
> Now, wenn storing the changes with $row->save( cascade => 1 ), the
> changes are stored as expected, _but_, a new "primary key" value is
> generated. So, for each update of this object, the value of the key
> field in the related table (CMP_NETWORK in this case) is incremented.

The primary key generator will only be called when attempting to
insert() an object that has an undef value in one or more of its
primary key columns.  Recall the following from the RDBO
documentation:

http://search.cpan.org/dist/Rose-DB-Object/lib/Rose/DB/Object.pm#Restriction
s

"Rose::DB::Object objects can represent rows in almost any database
table, subject to the following constraints.

* The database server must be supported by Rose::DB.
* The database table must have a primary key.
* The primary key must not allow null values in any of its columns."

If you require the ability to work with rows that have NULL values in
one or more of their primary key columns, then I suspect you ill have
some difficulty.

> Here's my primary key generator:
>
> sub default_primary_key_generator {
>     my( $class, $db ) = @_;
>
>     my $key     = $class->meta->primary_key->column_names->[0];
>
>     my %params  = (
>         query => [
>             $key => { like => 'MAT.'.$key.'.%' },
>         ],
>         sort_by => $key . ' DESC',
>         limit   => 1,
>     );
>
>     my $max_id = Rose::DB::Object::Manager->get_objects(
>                      object_class => $class,
>                      %params
>                  );
>
>     my $current = 0;
>
>     if ( $max_id->[0]->{$key} =~ m|^MAT\..*\.(\d+)$| ) {
>         $current = $1;
>     }
>     $current++;
>
>     my $id = 'MAT.'.$key.'.'.sprintf( '%06d', $current );
>
>     return $id;
>
> }   # --- end sub default_primary_key_generator ---

You should probably pass $db to your Manager call in the function
above, otherwise it will call $class->init_db to create a new one.

-John



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to