Six Apart Ltd. > Data-ObjectDriver-0.09 > Data::ObjectDriver::BaseObject

Download:
Data-ObjectDriver-0.09.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

New  9
Open  4
View/Report Bugs
Source  

NAME ^

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

SYNOPSIS ^

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

    __PACKAGE__->install_properties({
        columns     => [ 'ingredient_id', 'recipe_id', 'name', 'quantity' ],
        datasource  => 'ingredient',
        primary_key => [ 'recipe_id', 'ingredient_id' ],
        driver      => FoodDriver->driver,
    });

    __PACKAGE__->has_a(
        { class => 'Recipe', column => 'recipe_id', }
    );

    package main;

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

DESCRIPTION ^

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

CLASS DEFINITION ^

Class->install_properties(\%params)

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).

Class->has_a(@definitions)

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;
    __PACKAGE__->has_a(
        { 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:

Class->has_partitions(%param)

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.

BASIC USAGE ^

Class->lookup($id)

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.

$obj->exists()

Returns true if $obj already exists in the database.

$obj->save()

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

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

$obj->update()

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

$obj->insert()

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

$obj->remove()

Deletes $obj from its database.

$obj->replace()

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

USAGE ^

Class->new(%columns)

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

$obj->init(%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.

Class->properties()

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.

Class->driver()

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

Class->get_driver($get_driver_fn)

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().

Class->is_pkless()

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

Class->is_primary_key($column)

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

$obj->primary_key()

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

Class->primary_key_tuple()

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

$obj->is_same($other_obj)

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

$obj->object_is_stored()

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

$obj->pk_str()

returns the primay key has a printable string.

$obj->has_primary_key()

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

$obj->uncache_object()

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 explicitely, and this method provides you with a way to do it.

$obj->primary_key_to_terms([$id])

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.

Class->datasource()

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

Class->columns_of_type($type)

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.

$obj->set_values(\%values)

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

$obj->set_values_internal(\%values)

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

$obj->clone()

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

$obj->clone_all()

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

Class->has_column($column)

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

Class->column_names()

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

$obj->column_values()

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

$obj->is_changed([$column])

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

$obj->changed_cols_and_pk()

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.

$obj->changed_cols()

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).

Class->lookup_multi(\@ids)

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.

$obj->fetch_data()

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

$obj->refresh()

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.

$obj->column_func($column)

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.

$obj->deflate()

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.

Class->inflate($deflated)

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.

TRANSACTION SUPPORT AND METHODS ^

Introduction

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.

Class->begin_work

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.

Class->rollback

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

Class->commit

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

Class->txn_debug

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

Class->txn_active

Returns true if a transaction is already active.

DIAGNOSTICS ^

BUGS AND LIMITATIONS ^

There are no known bugs in this module.

SEE ALSO ^

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

LICENSE ^

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

AUTHOR & COPYRIGHT ^

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

syntax highlighting: