ZenMagick 0.9.10


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

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

Constant Summary
final static str

CACHE_KEY

Key used to cache table mappings.

Field Summary
static mixed

$NATIVE_TO_API_TYPEMAP

Mapping of native data types to API types.

Fields inherited from org.zenmagick.core\ZMObject
properties_
Method Summary
void

__construct(string configFolder)

Create a new instance.

protected array

addCustomFields(array mapping, string table, ZMDatabase database)

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

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
__construct, attachMethod, get, getAttachedMethods, getProperties, getPropertyNames, set, singleton

Constant Detail

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

CACHE_KEY

public final static str CACHE_KEY = "ZMDbTableMapper::mappings"

Key used to cache table mappings.


Field Detail

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

NATIVE_TO_API_TYPEMAP

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

Mapping of native data types to API types.


Method Detail

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

__construct

public void __construct(string configFolder)

Create a new instance.

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

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

addCustomFields

protected array addCustomFields(array mapping, string table, ZMDatabase database)

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

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

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

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 302

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 - The database.
Returns:
A mapping or null.

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

getCustomFieldInfo

public array getCustomFieldInfo(string table, ZMDatabase database)

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.
database - The database.
Returns:
A map of custom field details (if any)

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

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 385

getTableNames

public array getTableNames()

Get a list of all available tables.

Returns:
List of table names.

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

instance

public static void instance()

Get instance.


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

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 120

loadMappingFile

protected void loadMappingFile()

Load mappings from file.


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

onZMInitDone

public void onZMInitDone()

Refresh cache if empty.


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

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 142

updateCache

public void updateCache(ZMDatabase database)

Create/update cache.

Parameters:
database - The database.

ZenMagick 0.9.10