ZenMagick 0.9.6


org.zenmagick.database.ZMDbTableMapper

Class ZMDbTableMapper

ZMObject
|
+--ZMDbTableMapper

public class ZMDbTableMapper
extends ZMObject

Database table mapping *service*.

This class will read and parse file(s) containing model-database mappings. The code can map a single model class to a database table. Mappings are configured using nested arrays and wll be parsed into something ZMDatabase implementations can understand and use.

A simple mapping could look like this:

  'products_to_categories' => array(
      'productId' => 'column=products_id;type=integer;key=true',
      'categoryId' => 'column=categories_id;type=integer;key=true',
   ),

Each mapping has the table name as key (without any prefix) and an array of field mappings as value.

Each entry maps a single model property to a table column. The key is the model property name (starting lowercase). Available *attributes* are:/p>

column
The column in the table this model property is mapped to.
type
The data type. Supported types are:
  • integer - an integer
  • boolean - a boolean
  • string - a string
  • float - a float
  • date - a date in the format 'yyyy-mm-dd' (as defined by ZM_DATE_FORMAT)
  • datetime - a date and time value in the format 'yyyy-mm-dd hh:ii:ss' (as defined by ZM_DATETIME_FORMAT)
  • blob - binary data
key
If set to true this field is part of a table key.
auto
Indicates that this column is an auto-increment column, so new model instances will be updated with the new value on create.

Author:
DerManoMann
Version:
$Id: ZMDbTableMapper.php 2182 2009-04-28 03:21:16Z dermanomann $

Field Summary
final mixed

CACHE_KEY

static mixed

$NATIVE_TO_API_TYPEMAP

Mapping of native data types to API types.

Fields inherited from org.zenmagick.ZMObject
properties_
Constructor Summary

ZMDbTableMapper(string configFolder)

Create a new instance.

Method Summary
protected array

addCustomFields(array mapping, string table)

Add a field list of custom fields for the given table.

static array

buildTableMapping(string table, ZMDatabase database, boolean print)

Generate a database mapping for the given table.

array

ensureMapping(mixed mapping, ZMDatabase database)

Handle mixed mapping values.

static array

getCustomFieldInfo(string table)

Get field info map about custom fields for the given table.

array

getMapping(mixed tables, ZMDatabase database)

Get a table map.

array

getTableNames()

Get a list of all available tables.

static void

instance()

Get instance.

boolean

isCached()

Check if cached mappings are used.

protected void

loadMappingFile()

Load mappings from file.

void

onZMInitDone()

Refresh cache if empty.

protected array

parseTable(array mapping)

Parse mapping for a single table.

void

setMappingForTable(string table, mixed mapping, array The)

Set the mapping for the given table.

void

updateCache()

Create/update cache.

Methods inherited from org.zenmagick.ZMObject
attachMethod, get, getAttachedMethods, getPropertyNames, set, singleton

Field Detail

CACHE_KEY

public final mixed CACHE_KEY = "ZMDbTableMapper::mappings"

NATIVE_TO_API_TYPEMAP

public static mixed $NATIVE_TO_API_TYPEMAP = array( 'char' => 'string'

Mapping of native data types to API types. */


Constructor Detail

ZMDbTableMapper

public ZMDbTableMapper(string configFolder)

Create a new instance.

Parameters:
configFolder - The folder that contains the mapping files.

Method Detail

addCustomFields

protected array addCustomFields(array mapping, string table)

Add a field list of custom fields for the given table.

Parameters:
mapping - The existing mapping.
table - The table name.
Returns:
The updated mapping

buildTableMapping

public static array buildTableMapping(string table, ZMDatabase database, boolean print)

Generate a database mapping for the given table.

Parameters:
table - The table name.
database - Optional database; default is null to use the default.
print - Optional flag to also print the mapping in a form that can be used * to cut&paste into a mapping file; default is false.
Returns:
The mapping.

ensureMapping

public array ensureMapping(mixed mapping, ZMDatabase database)

Handle mixed mapping values.

If enabled (via setting 'isEnableDBAutoMapping'), mappings for unknown tables will be build on demand.

Parameters:
mapping - The field mappings or table name.
database - Optional database; default is null to use the default.
Returns:
A mapping or null.

getCustomFieldInfo

public static array getCustomFieldInfo(string table)

Get field info map about custom fields for the given table.

The returned array is a map with the property as key and an info map as value.

Parameters:
table - The table name.
Returns:
A map of custom field details (if any)

getMapping

public array getMapping(mixed tables, ZMDatabase database)

Get a table map.

Parameters:
tables - Either a single table or array of table names.
database - Optional database; default is null to use the default.
Returns:
The mapping or null.

getTableNames

public array getTableNames()

Get a list of all available tables.

Returns:
List of table names.

instance

public static void instance()

Get instance. /


isCached

public boolean isCached()

Check if cached mappings are used.

Returns:
true if mappings have been loaded from cache.

loadMappingFile

protected void loadMappingFile()

Load mappings from file. /


onZMInitDone

public void onZMInitDone()

Refresh cache if empty. /


parseTable

protected array parseTable(array mapping)

Parse mapping for a single table.

Parameters:
mapping - The mapping.
Returns:
The parsed mapping.

setMappingForTable

public void setMappingForTable(string table, mixed mapping, array The)

Set the mapping for the given table.

NOTE: This will silently override mappings for existing tables.

Parameters:
table - The (new) table.
The - new mapping.

updateCache

public void updateCache()

Create/update cache. /


ZenMagick 0.9.6