Rose::DB::Object::Helpers - A mix-in class containing convenience methods for Rose::DB::Object.


  package MyDBObject;

  use Rose::DB::Object;
  our @ISA = qw(Rose::DB::Object);

  use Rose::DB::Object::Helpers 'clone', 
    { load_or_insert => 'find_or_create' };

  $obj = MyDBObject->new(id => 123);

  $obj2 = $obj->clone;


Rose::DB::Object::Helpers provides convenience methods from use with Rose::DB::Object-derived classes. These methods do not exist in Rose::DB::Object in order to keep the method namespace clean. (Each method added to Rose::DB::Object is another potential naming conflict with a column accessor.)

This class inherits from Rose::DB::Object::MixIn. See the Rose::DB::Object::MixIn documentation for a full explanation of how to import methods from this class. The helper methods themselves are described below.



Returns a new object initialized with the column values of the existing object. For example, imagine a Person class with three columns, id, name, and age.

    $a = Person->new(id => 123, name => 'John', age => 30);

This use of the clone() method:

    $b = $a->clone;

is equivalent to this:

    $b = Person->new(id => $a->id, name => $a->name, age => $a->age);

This is the same as the clone method described above, except that it also sets all of the primary and unique key columns to undef. If the cloned object has a db attribute, then it is copied to the clone object as well.

For example, imagine a Person class with three columns, id, name, and age, where id is the primary key and name is a unique key.

    $a = Person->new(id => 123, name => 'John', age => 30, db => $db);

This use of the clone_and_reset() method:

    $b = $a->clone_and_reset;

is equivalent to this:

    $b = Person->new(id => $a->id, name => $a->name, age => $a->age);
    $b->id(undef);   # reset primary key
    $b->name(undef); # reset unique key
    $b->db($a->db);  # copy db
insert_or_update [PARAMS]

If the object already exists in the database, then update it. Otherwise, insert it. Any PARAMS are passed on to the calls to insert or update.

This method differs from the standard save method in that save decides to insert or update based solely on whether or not the object was previously loaded. This method will take the extra step of actually attempting to load the object to see whether or not it's in the database.

The return value of the insert or update method (whichever is called) is returned.

insert_or_update_on_duplicate_key [PARAMS]

Update or insert a row with a single SQL statement, depending on whether or not a row with the same primary or unique key already exists. Any PARAMS are passed on to the call to insert or update.

If the current database does not support the "ON DUPLICATE KEY UPDATE" SQL extension, then this method simply call the insert_or_update method, pasing all PARAMS.

Currently, the only database that supports "ON DUPLICATE KEY UPDATE" is MySQL, and only in version 4.1.0 or later. You can read more about the feature here:

Here's a quick example of the SQL syntax:

    INSERT INTO table (a, b, c) VALUES (1, 2, 3) 
      ON DUPLICATE KEY UPDATE a = 1, b = 2, c = 3;

Note that there are two sets of columns and values in the statement. This presents a choice: which columns to put in the "INSERT" part, and which to put in the "UPDATE" part.

When using this method, if the object was previously loaded from the database, then values for all columns are put in both the "INSERT" and "UPDATE" portions of the statement.

Otherwise, all columns are included in both clauses except those belonging to primary keys or unique keys which have only undefined values. This is important because it allows objects to be updated based on a single primary or unique key, even if other possible keys exist, but do not have values set. For example, consider this table with the following data:

    CREATE TABLE parts
      id      INT PRIMARY KEY,
      code    CHAR(3) NOT NULL,
      status  CHAR(1),


    INSERT INTO parts (id, code, status) VALUES (1, 'abc', 'x');

This code will update part id 1, setting its "status" column to "y".

    $p = Part->new(code => 'abc', status => 'y');

The resulting SQL:

    INSERT INTO parts (code, status) VALUES ('abc', 'y') 
      ON DUPLICATE KEY UPDATE code = 'abc', status = 'y';

Note that the "id" column is omitted because it has an undefined value. The SQL statement will detect the duplicate value for the unique key "code" and then run the "UPDATE" portion of the query, setting "status" to "y".

This method returns true if the row was inserted or updated successfully, false otherwise. The true value returned on success will be the object itself. If the object overloads its boolean value such that it is not true, then a true value will be returned instead of the object itself.

Yes, this method name is very long. Remember that you can rename methods on import. It is expected that most people will want to rename this method to "insert_or_update", using it in place of the normal insert_or_update helper method:

    package My::DB::Object;
    use Rose::DB::Object::Helpers 
      { insert_or_update_on_duplicate_key => 'insert_or_update' };
load_or_insert [PARAMS]

Try to load the object, passing PARAMS to the call to the load() method. The parameter "speculative => 1" is automatically added to PARAMS. If no such object is found, then the object is inserted.


    # Get object id 123 if it exists, otherwise create it now.
    $obj = MyDBObject->new(id => 123)->load_or_insert;
load_speculative [PARAMS]

Try to load the object, passing PARAMS to the call to the load() method along with the "speculative => 1" parameter. See the documentation for Rose::DB::Object's load method for more information.


    $obj = MyDBObject->new(id => 123);

      print "Found object id 123\n";
      print "Object id 123 not found\n";


John C. Siracusa (


Copyright (c) 2006 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.