Skip to content
Permalink
Browse files

Continue moving static features into the TableRegistry

Move a few more methods into the TableRegistry and change how static
configuration works. Instead of per table, it is now per alias. This
allows some different workflows.
  • Loading branch information...
markstory committed Oct 7, 2013
1 parent f9f5548 commit 7495ac75435bf53d24bd6e6025d2bda6ff5f3654
@@ -16,6 +16,9 @@
*/
namespace Cake\ORM;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
/**
* An Association is a relationship established between two tables and is used
* to configure and customize the way interconnected records are retrieved.
@@ -210,7 +213,7 @@ public function target(Table $table = null) {
if ($table === null) {
$className = $this->_className;
$this->_targetTable = Table::build($this->_name, compact('className'));
$this->_targetTable = TableRegistry::get($this->_name, compact('className'));
}
return $this->_targetTable;
}
@@ -19,6 +19,7 @@
use Cake\ORM\Association;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
/**
@@ -93,8 +94,7 @@ public function pivot($table = null) {
if (empty($this->_pivotTable)) {
$tableName = $this->_joinTableName();
$tableAlias = Inflector::classify(Inflector::singularize($tableName));
$table = Table::instance($tableAlias);
$table = $table ?: Table::build($tableAlias, [
$table = TableRegistry::get($tableAlias, [
'table' => $tableName
]);
} else {
@@ -103,7 +103,7 @@ public function pivot($table = null) {
}
if (is_string($table)) {
$table = Table::build($table);
$table = TableRegistry::get($table);
}
if (!$table->association($sAlias)) {
@@ -53,14 +53,6 @@ class Table {
*/
protected static $_instances = [];
/**
* A collection of default options to apply to each table built with the
* factory method. Indexed by table name
*
* @var array
*/
protected static $_tablesMap = [];
/**
* Name of the table as it can be found in the database
*
@@ -149,19 +141,10 @@ public function __construct(array $config = []) {
if (empty($config['table'])) {
$config['table'] = Inflector::tableize(get_class($this));
}
if (!empty($config['table'])) {
$this->table($config['table']);
}
$this->table($config['table']);
if (!empty($config['alias'])) {
$this->alias($config['alias']);
}
$table = $this->table();
if (isset(static::$_tablesMap[$table])) {
$config = array_merge(static::$_tablesMap[$table], $config);
}
if (!empty($config['connection'])) {
$this->connection($config['connection']);
}
@@ -200,44 +183,6 @@ public function __construct(array $config = []) {
public function initialize(array $config) {
}
/**
* A factory method to build a new Table instance. Once created, the instance
* will be registered so it can be re-used if tried to build again for the same
* alias.
*
* The options that can be passed are the same as in `__construct()`, but the
* key `className` is also recognized.
*
* When $options contains `className` this method will try to instantiate an
* object of that class instead of this default Table class.
*
* If no `table` option is passed, the table name will be the tableized version
* of the provided $alias.
*
* @param string $alias the alias for the table
* @param array $options a list of options as accepted by `__construct()`
* @return Table
*/
public static function build($alias, array $options = []) {
return TableRegistry::get($alias, $options);
}
/**
* Returns the Table object associated to the provided alias, if any.
* If a Table is passed as second parameter it will be associated to the
* passed $alias even if another object was associated before.
*
* @param string $alias
* @param Table $object
* @return Table
*/
public static function instance($alias, self $object = null) {
if ($object === null) {
return TableRegistry::get($alias);
}
return TableRegistry::set($alias, $object);
}
/**
* Returns the database table name or sets a new one
*
@@ -1,6 +1,7 @@
<?php
namespace Cake\ORM;
use Cake\Core\App;
use Cake\Utility\Inflector;
/**
@@ -26,18 +27,18 @@ class TableRegistry {
* all tables
*
* @param string $table name of the table
* @param array $options list of options for the table
* @param null|array $options list of options for the table
* @return array
*/
public static function config($table, $data) {
public static function config($table = null, $options = null) {
if ($table === null) {
return static::$_config;
}
if (!is_string($table)) {
return static::$_config = $table;
}
if ($options === null) {
return isset(static::$_config[$table]) ? static::$_tablesMap[$table] : [];
return isset(static::$_config[$table]) ? static::$_config[$table] : [];
}
return static::$_config[$table] = $options;
}
@@ -80,6 +81,9 @@ public static function get($alias, $options = []) {
$options['className'] = $className;
}
if (isset(static::$_config[$alias])) {
$options = array_merge(static::$_config[$alias], $options);
}
return static::$_instances[$alias] = new $options['className']($options);
}
@@ -19,23 +19,17 @@
use Cake\Core\Configure;
use Cake\Database\ConnectionManager;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
/**
* Used to test correct class is instantiated when using Table::build();
* Used to test correct class is instantiated when using TableRegistry::get();
*/
class UsersTable extends Table {
/**
* Overrides default table name
*
* @var string
*/
protected $_table = 'users';
}
/**
* Used to test correct class is instantiated when using Table::build();
* Used to test correct class is instantiated when using TableRegistry::get();
*/
class MyUsersTable extends Table {
@@ -67,45 +61,44 @@ public function setUp() {
public function tearDown() {
parent::tearDown();
Table::clearRegistry();
Configure::write('App.namespace', 'App');
TableRegistry::clear();
}
/**
* Tests that table options can be pre-configured for the factory method
*
* @return void
*/
public function testConfigAndBuild() {
Table::clearRegistry();
$map = Table::config();
TableRegistry::clear();
$map = TableRegistry::config();
$this->assertEquals([], $map);
$options = ['connection' => $this->connection];
Table::config('users', $options);
$map = Table::config();
TableRegistry::config('users', $options);
$map = TableRegistry::config();
$this->assertEquals(['users' => $options], $map);
$this->assertEquals($options, Table::config('users'));
$this->assertEquals($options, TableRegistry::config('users'));
$schema = ['id' => ['type' => 'rubbish']];
$options += ['schema' => $schema];
Table::config('users', $options);
TableRegistry::config('users', $options);
$table = Table::build('foo', ['table' => 'users']);
$table = TableRegistry::get('users', ['table' => 'users']);
$this->assertInstanceOf('Cake\ORM\Table', $table);
$this->assertEquals('users', $table->table());
$this->assertEquals('foo', $table->alias());
$this->assertEquals('users', $table->alias());
$this->assertSame($this->connection, $table->connection());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
$this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
Table::clearRegistry();
$this->assertEmpty(Table::config());
TableRegistry::clear();
$this->assertEmpty(TableRegistry::config());
Table::config('users', $options);
$table = Table::build('foo', ['className' => __NAMESPACE__ . '\MyUsersTable']);
TableRegistry::config('users', $options);
$table = TableRegistry::get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
$this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
$this->assertEquals('users', $table->table());
$this->assertEquals('foo', $table->alias());
$this->assertEquals('users', $table->alias());
$this->assertSame($this->connection, $table->connection());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
}
@@ -117,34 +110,22 @@ public function testConfigAndBuild() {
* @return void
*/
public function testBuildConvention() {
$table = Table::build('article');
$table = TableRegistry::get('article');
$this->assertInstanceOf('\TestApp\Model\Repository\ArticleTable', $table);
$table = Table::build('Article');
$table = TableRegistry::get('Article');
$this->assertInstanceOf('\TestApp\Model\Repository\ArticleTable', $table);
$table = Table::build('author');
$table = TableRegistry::get('author');
$this->assertInstanceOf('\TestApp\Model\Repository\AuthorTable', $table);
$table = Table::build('Author');
$table = TableRegistry::get('Author');
$this->assertInstanceOf('\TestApp\Model\Repository\AuthorTable', $table);
$class = $this->getMockClass('\Cake\ORM\Table');
class_alias($class, 'MyPlugin\Model\Repository\SuperTestTable');
$table = Table::build('MyPlugin.SuperTest');
$table = TableRegistry::get('MyPlugin.SuperTest');
$this->assertInstanceOf($class, $table);
}
/**
* Tests getting and setting a Table instance in the registry
*
* @return void
*/
public function testInstance() {
$this->assertNull(Table::instance('users'));
$table = new Table(['table' => 'users']);
Table::instance('users', $table);
$this->assertSame($table, Table::instance('users'));
}
/**
* Tests the table method
*
@@ -583,7 +564,7 @@ public function testDeleteAllFailure() {
$table = $this->getMock(
'Cake\ORM\Table',
['_buildQuery'],
[['table' => 'users']]
[['table' => 'users', 'connection' => $this->connection]]
);
$query = $this->getMock('Cake\ORM\Query', ['executeStatement'], [$this->connection, null]);
$table->expects($this->once())

0 comments on commit 7495ac7

Please sign in to comment.
You can’t perform that action at this time.