Skip to content

Commit

Permalink
Refactoring the table factory to make it a bit more useful in the way
Browse files Browse the repository at this point in the history
you can store defaults in advance and allowing more control for
subclasses if they are used
  • Loading branch information
lorenzo committed Jun 9, 2013
1 parent c7f2ac3 commit 248174a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
31 changes: 19 additions & 12 deletions lib/Cake/ORM/Table.php
Expand Up @@ -108,13 +108,16 @@ class Table {
*/
public function __construct($config = array()) {
if (!empty($config['table'])) {
$this->_table = $config['table'];
$this->table($config['table']);
}

if (!empty($config['alias'])) {
$this->alias($config['alias']);
} else {
$this->alias($this->_table);
}

$table = $this->table();
if (isset(static::$_tablesMap[$table])) {
$config = array_merge(static::$_tablesMap[$table], $config);
}

if (!empty($config['connection'])) {
Expand Down Expand Up @@ -149,15 +152,6 @@ public static function build($alias, array $options = []) {
}

$options = ['alias' => $alias] + $options;

if (empty($options['table'])) {
$options['table'] = Inflector::tableize($alias);
}

if (isset(static::$_tablesMap[$options['table']])) {
$options = array_merge(static::$_tablesMap[$options['table']], $options);
}

if (empty($options['className'])) {
$options['className'] = get_called_class();
}
Expand Down Expand Up @@ -228,6 +222,14 @@ public function table($table = null) {
if ($table !== null) {
$this->_table = $table;
}
if ($this->_table === null) {
$table = explode('\\', get_class($this));
$table = substr(end($table), 0, -5);
if (empty($table)) {
$table = $this->alias();
}
$this->_table = Inflector::tableize($table);
}
return $this->_table;
}

Expand All @@ -242,6 +244,11 @@ public function alias($alias = null) {
$this->_alias = $alias;
static::instance($alias, $this);
}
if ($this->_alias === null) {
$alias = explode('\\', get_class($this));
$alias = substr(end($alias), 0, -5) ?: $this->_table;
$this->_alias = $alias;
}
return $this->_alias;
}

Expand Down
14 changes: 10 additions & 4 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -26,6 +26,13 @@
**/
class DatesTable extends Table {

/**
* Overrides default table name
*
* @var string
*/
public $_table = 'my_dates';

}

/**
Expand Down Expand Up @@ -130,11 +137,10 @@ public function testConfigAndBuild() {
Table::clearRegistry();
$this->assertEmpty(Table::config());

$options['className'] = __NAMESPACE__ . '\DatesTable';
Table::config('dates', $options);
$table = Table::build('foo', ['table' => 'dates']);
Table::config('my_dates', $options);
$table = Table::build('foo', ['className' => __NAMESPACE__ . '\DatesTable']);
$this->assertInstanceOf(__NAMESPACE__ . '\DatesTable', $table);
$this->assertEquals('dates', $table->table());
$this->assertEquals('my_dates', $table->table());
$this->assertEquals('foo', $table->alias());
$this->assertSame($this->connection, $table->connection());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
Expand Down

0 comments on commit 248174a

Please sign in to comment.