View on
Six Apart > Data-ObjectDriver-0.15 > Data::ObjectDriver::BaseObject



Annotate this POD


View/Report Bugs


Data::ObjectDriver::BaseObject - base class for modeled objects


    package Ingredient;
    use base qw( Data::ObjectDriver::BaseObject );

        columns     => [ 'ingredient_id', 'recipe_id', 'name', 'quantity' ],
        datasource  => 'ingredient',
        primary_key => [ 'recipe_id', 'ingredient_id' ],
        driver      => FoodDriver->driver,

        { class => 'Recipe', column => 'recipe_id', }

    package main;

    my ($ingredient) = Ingredient->search({ recipe_id => 4, name => 'rutabaga' });


Data::ObjectDriver::BaseObject provides services to data objects modeled with the Data::ObjectDriver object relational mapper.



Defines all the properties of the specified object class. Generally you should call install_properties() in the body of your class definition, so the properties can be set when the class is used or required.

Required members of %params are:

The optional members of %params are:

Custom object drivers may define other properties for your object classes. Consult the documentation of those object drivers for more information.

Class->install_column($col, $def)

Modify the Class definition to declare a new column $col of definition <$def> (see column_defs).


NOTE: has_a is an experimental system, likely to both be buggy and change in future versions.

Defines a foreign key reference between two classes, creating accessor methods to retrieve objects both ways across the reference. For each defined reference, two methods are created: one for objects of class Class to load the objects they reference, and one for objects of the referenced class to load the set of Class objects that reference them.

For example, this definition:

    package Ingredient;
        { class => 'Recipe', column => 'recipe_id' },

would create Ingredient->recipe_obj and Recipe->ingredient_objs instance methods.

Each member of @definitions is a hashref containing the parameters for creating one accessor method. The required members of these hashes are:

The optional members of has_a() definitions are:


Defines that the given class is partitioned, configuring it for use with the Data::ObjectDriver::Driver::SimplePartition object driver. Required members of %param are:

Note that only the parent object for use with the SimplePartition driver should use has_partitions(). See Data::ObjectDriver::Driver::SimplePartition for more about partitioning.



Returns the instance of Class with the given value for its primary key. If Class has a complex primary key (more than one column), $id should be an arrayref specifying the column values in the same order as specified in the primary_key property.

Class->search(\%terms, [\%args])

Returns all instances of Class that match the values specified in \%terms, keyed on column names. In list context, search returns the objects containing those values. In scalar context, search returns an iterator function containing the same set of objects.

Your search can be customized with parameters specified in \%args. Commonly recognized parameters (those implemented by the standard Data::ObjectDriver object drivers) are:

All options are passed to the object driver, so your driver may support additional options.

Class->result(\%terms, [\%args])

Takes the same %terms and %args arguments that search takes, but instead of executing the query immediately, returns a Data::ObjectDriver::ResultSet object representing the set of results.


Returns true if $obj already exists in the database.


Saves $obj to the database, whether it is already there or not. That is, save() is functionally:

    $obj->exists() ? $obj->update() : $obj->insert()


Saves changes to $obj, an object that already exists in its database.


Adds $obj to the database in which it should exist, according to its object driver and configuration.


Deletes $obj from its database.


Replaces $obj in the database. Does the right thing if the driver knows how to REPLACE object, ala MySQL.



Returns a new object of the given class, initializing its columns with the values in %columns.


Initializes $obji by initializing its columns with the values in %columns.

Override this method if you must do initial configuration to new instances of $obj's class that are not more appropriate as a post_load callback.


Returns the named object class's properties as a hashref. Note that some of the standard object class properties, such as primary_key, have more convenient accessors than reading the properties directly.


Returns the object driver for this class, invoking the class's get_driver function (and caching the result for future calls) if necessary.


Sets the function used to find the object driver for Class objects (that is, the get_driver property).

Note that once driver() has been called, the get_driver function is not used. Usually you would specify your function as the get_driver parameter to install_properties().


Returns whether the given object class has a primary key defined.


Returns whether the given column is or is part of the primary key for Class objects.


Returns the values of the primary key fields of $obj.


Returns the names of the primary key fields of Class objects.


Do a primary key check on $obj and $<other_obj> and returns true only if they are identical.


Returns true if the object hasn't been stored in the database yet. This is particularly useful in triggers where you can then determine if the object is being INSERTED or just UPDATED.


returns the primary key has a printable string.


Returns whether the given object has values for all of its primary key fields.


If you use a Cache driver, returned object will be automatically cached as a result of common retrieve operations. In some rare cases you may want the cache to be cleared explicitly, and this method provides you with a way to do it.


Returns $obj's primary key as a hashref of values keyed on column names, suitable for passing as search() terms. If $id is specified, convert that primary key instead of $obj's.


Returns the datasource for objects of class Class. That is, returns the datasource property of Class.


Returns the list of columns in Class objects that hold data of type $type, as an arrayref. Columns are of a certain type when they are set that way in Class's column_defs property.


Sets all the columns of $obj that are members of \%values to the values specified there.


Sets new specified values of $obj, without using any overridden mutator methods of $obj and without marking the changed columns changed.


Returns a new object of the same class as $obj containing the same data, except for primary keys, which are set to undef.


Returns a new object of the same class as $obj containing the same data, including all key fields.


Returns whether a column named $column exists in objects of class <Class>.


Returns the list of columns in Class objects as an arrayref.


Returns the columns and values in the given object as a hashref.

$obj->column($column, [$value])

Returns the value of $obj's column $column. If $value is specified, column() sets the first.

Note the usual way of accessing and mutating column values is through the named accessors:

    $obj->column('fred', 'barney');  # possible
    $obj->fred('barney');            # preferred


Returns whether any values in $obj have changed. If $column is given, returns specifically whether that column has changed.


Returns the list of all columns that have changed in $obj since it was last loaded from or saved to the database, as a list.


Returns the list of changed columns in $obj as a list, except for any columns in $obj's primary key (even if they have changed).


Returns a list (arrayref) of objects as specified by their primary keys.

Class->bulk_insert(\@columns, \@data)

Adds the given data, an arrayref of arrayrefs containing column values in the order of column names given in \@columns, as directly to the database as Class records.

Note that only some database drivers (for example, Data::ObjectDriver::Driver::DBD::Pg) implement the bulk insert operation.


Returns the current values from $obj as saved in the database, as a hashref.


Resets the values of $obj from the database. Any unsaved modifications to $obj will be lost, and any made meanwhile will be reflected in $obj afterward.


Creates an accessor/mutator method for column $column, returning it as a coderef.

Override this if you need special behavior in all accessor/mutator methods.


Returns a minimal representation of the object, for use in caches where you might want to preserve space (like memcached). Can also be overridden by subclasses to store the optimal representation of an object in the cache. For example, if you have metadata attached to an object, you might want to store that in the cache, as well.


Inflates the deflated representation of the object $deflated into a proper object in the class Class. That is, undoes the operation $deflated = $obj->deflate() by returning a new object equivalent to $obj.



When dealing with the methods on this class, the transactions are global, i.e: applied to all drivers. You can still enable transactions per driver if you directly use the driver API.


This enable transactions globally for all drivers until the next rollback or commit call on the class.

If begin_work is called while a transaction is still active (nested transaction) then the two transactions are merged. So inner transactions are ignored and a warning will be emitted.


This rollbacks all the transactions since the last begin work, and exits from the active transaction state.


Commits the transactions, and exits from the active transaction state.


Just return the value of the global flag and the current working drivers in a hashref.


Returns true if a transaction is already active.



There are no known bugs in this module.


Data::ObjectDriver, Data::ObjectDriver::Driver::DBI, Data::ObjectDriver::Driver::SimplePartition


Data::ObjectDriver is free software; you may redistribute it and/or modify it under the same terms as Perl itself.


Except where otherwise noted, Data::ObjectDriver is Copyright 2005-2006 Six Apart, All rights reserved.

syntax highlighting: