ZenMagick 0.9.8


org.zenmagick.core.services.database.ZMDbTableMapper
\core\services\database\ZMDbTableMapper.php at line 67

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 2610 2009-11-20 02:45:25Z dermanomann $

Field Summary
final str

CACHE_KEY

Key used to cache table mappings.

static mixed

$NATIVE_TO_API_TYPEMAP

Mapping of native data types to API types.

Fields inherited from org.zenmagick.core.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.

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.

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(ZMDatabase database)

Create/update cache.

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

Field Detail

\core\services\database\ZMDbTableMapper.php at line 87

CACHE_KEY

public final str CACHE_KEY = "ZMDbTableMapper::mappings"

Key used to cache table mappings.


\core\services\database\ZMDbTableMapper.php at line 69

NATIVE_TO_API_TYPEMAP

public static mixed $NATIVE_TO_API_TYPEMAP = array(...)

Mapping of native data types to API types.


Constructor Detail

\core\services\database\ZMDbTableMapper.php at line 99

ZMDbTableMapper

public ZMDbTableMapper(string configFolder)

Create a new instance.

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

Method Detail

\core\services\database\ZMDbTableMapper.php at line 367

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

\core\services\database\ZMDbTableMapper.php at line 248

buildTableMapping

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

Generate a database mapping for the given table.

Parameters:
table - The table name.
database - The database.
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.

\core\services\database\ZMDbTableMapper.php at line 303

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.

\core\services\database\ZMDbTableMapper.php at line 340

getCustomFieldInfo

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

\core\services\database\ZMDbTableMapper.php at line 207

getMapping

public array getMapping(mixed tables, ZMDatabase database)

Get a table map.

Parameters:
tables - Either a single table or array of table names.
database - The database.
Returns:
The mapping or null.

\core\services\database\ZMDbTableMapper.php at line 382

getTableNames

public array getTableNames()

Get a list of all available tables.

Returns:
List of table names.

\core\services\database\ZMDbTableMapper.php at line 121

instance

public static void instance()

Get instance.


\core\services\database\ZMDbTableMapper.php at line 151

isCached

public boolean isCached()

Check if cached mappings are used.

Returns:
true if mappings have been loaded from cache.

\core\services\database\ZMDbTableMapper.php at line 138

loadMappingFile

protected void loadMappingFile()

Load mappings from file.


\core\services\database\ZMDbTableMapper.php at line 129

onZMInitDone

public void onZMInitDone()

Refresh cache if empty.


\core\services\database\ZMDbTableMapper.php at line 179

parseTable

protected array parseTable(array mapping)

Parse mapping for a single table.

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

\core\services\database\ZMDbTableMapper.php at line 328

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.

\core\services\database\ZMDbTableMapper.php at line 160

updateCache

public void updateCache(ZMDatabase database)

Create/update cache.

Parameters:
database - The database.

ZenMagick 0.9.8