Permalink
Browse files

BackwardsCompatibilityBreak - fORM::addCustomClassTableMapping() was …

…renamed to fORM::mapClassToTable()

Fixed tickets #148 and #265 - Added the ability to set a database name via fORMDatabase::attach() and fORMSchema::attach() which can then be used to support multiple databases for the ORM via fORM::mapClassToDatabase(). In addition, different databases can be set for read and write operations in fORMDatabase::attach() and fORMSchema::attach(), which allows for master/slave setups.
  • Loading branch information...
wbond committed Nov 3, 2009
1 parent bd2163c commit b55d0d4314a4ba851228b8e8648ec260ce4a602f
View
@@ -15,7 +15,8 @@
* @package Flourish
* @link http://flourishlib.com/fActiveRecord
*
* @version 1.0.0b47
* @version 1.0.0b48
* @changes 1.0.0b48 Updated code for the new fORMDatabase and fORMSchema APIs [wb, 2009-10-28]
* @changes 1.0.0b47 Changed `::associate{RelatedRecords}()`, `::link{RelatedRecords}()` and `::populate{RelatedRecords}()` to allow for method chaining [wb, 2009-10-22]
* @changes 1.0.0b46 Changed SQL statements to use value placeholders and identifier escaping [wb, 2009-10-22]
* @changes 1.0.0b45 Added support for `!~`, `&~`, `><` and OR comparisons to ::checkConditions(), made object handling in ::checkConditions() more robust [wb, 2009-09-21]
@@ -537,7 +538,9 @@ static public function hash($record, $class=NULL)
$class = get_class($record);
}
$pk_columns = fORMSchema::retrieve()->getKeys(fORM::tablize($class), 'primary');
$schema = fORMSchema::retrieve($class);
$table = fORM::tablize($class);
$pk_columns = $schema->getKeys($table, 'primary');
// Build an array of just the primary key data
$pk_data = array();
@@ -771,7 +774,7 @@ public function __call($method_name, $parameters)
$plural = FALSE;
// one-to-many relationships need to use plural forms
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
if (in_array($subject, $schema->getTables())) {
if (fORMSchema::isOneToOne($schema, $table, $subject, $route)) {
throw new fProgrammerException(
@@ -863,7 +866,7 @@ public function __call($method_name, $parameters)
$route = isset($parameters[0]) ? $parameters[0] : NULL;
// one-to-many relationships need to use plural forms
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
if (in_array($subject, $schema->getTables())) {
if (fORMSchema::isOneToOne($schema, $table, $subject, $route)) {
throw new fProgrammerException(
@@ -956,7 +959,7 @@ public function __clone()
}
// If we have a single auto incrementing primary key, remove the value
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
$table = fORM::tablize($class);
$pk_columns = $schema->getKeys($table, 'primary');
@@ -978,7 +981,7 @@ public function __clone()
public function __construct($key=NULL)
{
$class = get_class($this);
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
// If the features of this class haven't been set yet, do it
if (!isset(self::$configured[$class])) {
@@ -1171,7 +1174,7 @@ protected function constructInsertParams($new_autoincrementing_record, $pk_colum
$value_placeholders = array();
$class = get_class($this);
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
$table = fORM::tablize($class);
$column_info = $schema->getColumnInfo($table);
foreach ($column_info as $column => $info) {
@@ -1209,7 +1212,7 @@ protected function constructInsertParams($new_autoincrementing_record, $pk_colum
protected function constructUpdateParams()
{
$class = get_class($this);
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
$table = fORM::tablize($class);
$column_info = $schema->getColumnInfo($table);
@@ -1262,8 +1265,8 @@ public function delete()
);
}
$db = fORMDatabase::retrieve();
$schema = fORMSchema::retrieve();
$db = fORMDatabase::retrieve($class, 'write');
$schema = fORMSchema::retrieve($class);
fORM::callHookCallbacks(
$this,
@@ -1468,8 +1471,9 @@ protected function encode($column, $formatting=NULL)
);
}
$schema = fORMSchema::retrieve();
$table = fORM::tablize(get_class($this));
$class = get_class($this);
$schema = fORMSchema::retrieve($class);
$table = fORM::tablize($class);
$column_type = $schema->getColumnInfo($table, $column, 'type');
// Ensure the programmer is calling the function properly
@@ -1553,7 +1557,7 @@ public function exists()
return $this->__call('exists', array());
}
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
$table = fORM::tablize($class);
$pk_columns = $schema->getKeys($table, 'primary');
$exists = FALSE;
@@ -1581,8 +1585,8 @@ protected function fetchResultFromUniqueKey($values)
{
$class = get_class($this);
$db = fORMDatabase::retrieve();
$schema = fORMSchema::retrieve();
$db = fORMDatabase::retrieve($class, 'read');
$schema = fORMSchema::retrieve($class);
try {
if ($values === array_combine(array_keys($values), array_fill(0, sizeof($values), NULL))) {
@@ -1651,7 +1655,7 @@ protected function inspect($column, $element=NULL)
$class = get_class($this);
$table = fORM::tablize($class);
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
$info = $schema->getColumnInfo($table, $column);
if (!in_array($info['type'], array('varchar', 'char', 'text'))) {
@@ -1696,8 +1700,8 @@ protected function inspect($column, $element=NULL)
public function load()
{
$class = get_class($this);
$db = fORMDatabase::retrieve();
$schema = fORMSchema::retrieve();
$db = fORMDatabase::retrieve($class, 'read');
$schema = fORMSchema::retrieve($class);
if (fORM::getActiveRecordMethod($class, 'load')) {
return $this->__call('load', array());
@@ -1737,8 +1741,8 @@ protected function loadFromResult($result, $ignore_identity_map=FALSE)
$table = fORM::tablize($class);
$row = $result->current();
$db = fORMDatabase::retrieve();
$schema = fORMSchema::retrieve();
$db = fORMDatabase::retrieve($class, 'read');
$schema = fORMSchema::retrieve($class);
if (!isset(self::$unescape_map[$class])) {
self::$unescape_map[$class] = array();
@@ -1846,7 +1850,7 @@ public function populate()
$this->cache
);
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
$table = fORM::tablize($class);
$column_info = $schema->getColumnInfo($table);
@@ -1897,7 +1901,7 @@ protected function prepare($column, $formatting=NULL)
$class = get_class($this);
$table = fORM::tablize($class);
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
$column_info = $schema->getColumnInfo($table, $column);
$column_type = $column_info['type'];
@@ -1990,7 +1994,7 @@ public function reflect($include_doc_comments=FALSE)
$class = get_class($this);
$table = fORM::tablize($class);
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
$columns_info = $schema->getColumnInfo($table);
foreach ($columns_info as $column => $column_info) {
$camelized_column = fGrammar::camelize($column, TRUE);
@@ -2257,7 +2261,7 @@ public function replicate($related_class=NULL)
$class = get_class($this);
$hash = self::hash($this->values, $class);
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
$table = fORM::tablize($class);
// If the object has not been replicated yet, do it now
@@ -2406,7 +2410,7 @@ protected function set($column, $value)
// Float and int columns that look like numbers with commas will have the commas removed
if (is_string($value)) {
$table = fORM::tablize($class);
$schema = fORMSchema::retrieve();
$schema = fORMSchema::retrieve($class);
$type = $schema->getColumnInfo($table, $column, 'type');
if (in_array($type, array('integer', 'float')) && preg_match('#^(\d+,)+\d+(\.\d+)?$#', $value)) {
$value = str_replace(',', '', $value);
@@ -2446,8 +2450,8 @@ public function store()
$this->cache
);
$db = fORMDatabase::retrieve();
$schema = fORMSchema::retrieve();
$db = fORMDatabase::retrieve($class, 'write');
$schema = fORMSchema::retrieve($class);
try {
$table = fORM::tablize($class);
View
@@ -46,7 +46,8 @@
* @package Flourish
* @link http://flourishlib.com/fDatabase
*
* @version 1.0.0b19
* @version 1.0.0b20
* @changes 1.0.0b20 Added a parameter to ::enableCaching() to provide a key token that will allow cached values to be shared between multiple databases with the same schema [wb, 2009-10-28]
* @changes 1.0.0b19 Added support for escaping identifiers (column and table names) to ::escape(), added support for database schemas, rewrote internal SQL string spliting [wb, 2009-10-22]
* @changes 1.0.0b18 Updated the class for the new fResult and fUnbufferedResult APIs, fixed ::unescape() to not touch NULLs [wb, 2009-08-12]
* @changes 1.0.0b17 Added the ability to pass an array of all values as a single parameter to ::escape() instead of one value per parameter [wb, 2009-08-11]
@@ -99,6 +100,13 @@ static protected function compose($message)
*/
private $cache;
/**
* The cache prefix to use for cache entries
*
* @var string
*/
private $cache_prefix;
/**
* Database connection resource or PDO object
*
@@ -739,13 +747,18 @@ protected function determineExtension()
/**
* Sets the schema info to be cached to the fCache object specified
*
* @param fCache $cache The cache to cache to
* @param fCache $cache The cache to cache to
* @param string $key_token Internal use only! (this will be used in the cache key to uniquely identify the cache for this fDatabase object)
* @return void
*/
public function enableCaching($cache)
public function enableCaching($cache, $key_token=NULL)
{
$this->cache = $cache;
if ($key_token !== NULL) {
$this->cache_prefix = 'fDatabase::' . $this->type . '::' . $key_token . '::';
}
$this->schema_info = $this->cache->get($this->makeCachePrefix() . 'schema_info', array());
}
@@ -2003,22 +2016,25 @@ public function isInsideTransaction()
/**
* Creates a unique cache prefix to help prevent cache conflicts
*
* @return void
* @return string The cache prefix to use
*/
private function makeCachePrefix()
{
$prefix = 'fDatabase::' . $this->type . '::';
if ($this->host) {
$prefix .= $this->host . '::';
}
if ($this->port) {
$prefix .= $this->port . '::';
}
$prefix .= $this->database . '::';
if ($this->username) {
$prefix .= $this->username . '::';
if (!$this->cache_prefix) {
$prefix = 'fDatabase::' . $this->type . '::';
if ($this->host) {
$prefix .= $this->host . '::';
}
if ($this->port) {
$prefix .= $this->port . '::';
}
$prefix .= $this->database . '::';
if ($this->username) {
$prefix .= $this->username . '::';
}
}
return $prefix;
return $this->cache_prefix;
}
View
@@ -9,7 +9,8 @@
* @package Flourish
* @link http://flourishlib.com/fGrammar
*
* @version 1.0.0b4
* @version 1.0.0b5
* @changes 1.0.0b5 Fixed ::reset() to properly reset the singularization and pluralization rules [wb, 2009-10-28]
* @changes 1.0.0b4 Added caching for various methods - provided significant performance boost to ORM [wb, 2009-06-15]
* @changes 1.0.0b3 Changed replacement values in preg_replace() calls to be properly escaped [wb, 2009-06-11]
* @changes 1.0.0b2 Fixed a bug where some words would lose capitalization with ::pluralize() and ::singularize() [wb, 2009-01-25]
@@ -448,10 +449,10 @@ static public function reset()
self::$plural_to_singular_rules = array(
'([ml])ice' => '\1ouse',
'(media|info(rmation)?|news)$' => '\1',
'quizzes$' => 'quiz',
'children$' => 'child',
'people$' => 'person',
'men$' => 'man',
'(q)uizzes$' => '\1uiz',
'(c)hildren$' => '\1hild',
'(p)eople$' => '\1erson',
'(m)en$' => '\1an',
'((?!sh).)oes$' => '\1o',
'((?<!o)[ieu]s|[ieuo]x)es$' => '\1',
'([cs]h)es$' => '\1',
@@ -469,10 +470,10 @@ static public function reset()
'([ml])ouse$' => '\1ice',
'(media|info(rmation)?|news)$' => '\1',
'(phot|log)o$' => '\1os',
'^(q)uiz$' => 'quizzes',
'child$' => 'children',
'person$' => 'people',
'man$' => 'men',
'^(q)uiz$' => '\1uizzes',
'(c)hild$' => '\1hildren',
'(p)erson$' => '\1eople',
'(m)an$' => '\1en',
'([ieu]s|[ieuo]x)$' => '\1es',
'([cs]h)$' => '\1es',
'(ss)$' => '\1es',
Oops, something went wrong.

0 comments on commit b55d0d4

Please sign in to comment.