Skip to content

Commit

Permalink
Small refactoring and beginnings of a flyweight for table objects that
Browse files Browse the repository at this point in the history
will replace ClassRegistry
  • Loading branch information
lorenzo committed Apr 27, 2013
1 parent 6da7fc4 commit 013b246
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
59 changes: 53 additions & 6 deletions lib/Cake/ORM/Table.php
Expand Up @@ -18,7 +18,11 @@

class Table {

protected $_name;
protected static $_instances = [];

protected static $_tablesMap = [];

protected $_table;

protected $_alias;

Expand All @@ -27,14 +31,14 @@ class Table {
protected $_schema;

public function __construct($config = array()) {
if (!empty($config['name'])) {
$this->_name = $config['name'];
if (!empty($config['table'])) {
$this->_table = $config['table'];
}

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

if (!empty($config['connection'])) {
Expand All @@ -45,6 +49,49 @@ public function __construct($config = array()) {
}
}

public static function build($alias, array $options = []) {
if (isset(static::$_instances[$alias])) {
return static::$_instances[$alias];
}
if (!empty($options['table']) && isset(static::$_tablesMap[$options['table']])) {
$options = array_merge(static::$_tablesMap[$options['table']], $options);
}

$options = ['alias' => $alias] + $options;
if (empty($options['className'])) {
$options['className'] = get_called_class();
}

return static::$_instances[$alias] = new $options['className']($options);
}

public static function map($alias = null, array $options = null) {
if ($alias === null) {
return static::$_tablesMap;
}
if (!is_string($alias)) {
static::$_tablesMap = $alias;
return;
}
if ($options === null) {
return isset(static::$_tablesMap[$alias]) ? static::$_tablesMap[$alias] : null;
}
static::$_tablesMap[$alias] = $options;
}

public function clearRegistry() {
static::$_instances = [];
static::$_tablesMap = [];
}

public function table($table = null) {
if ($table !== null) {
$this->_table = $table;
}
return $this->_table;
}


public function alias($alias = null) {
if ($alias !== null) {
$this->_alias = $alias;
Expand All @@ -62,7 +109,7 @@ public function connection($conn = null) {
public function schema($schema = null) {
if ($schema === null) {
if ($this->_schema === null) {
$this->_schema = $this->connection()->describe($this->_name);
$this->_schema = $this->connection()->describe($this->_table);
}
return $this->_schema;
}
Expand All @@ -86,7 +133,7 @@ protected function buildQuery() {
return $query
->repository($this)
->select()
->from([$this->_alias => $this->_name]);
->from([$this->_alias => $this->_table]);
}

}
30 changes: 27 additions & 3 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -91,9 +91,32 @@ protected function _createDatesTable() {
return $result;
}

/**
* Tests that table options can be pre-configured for the factory method
*
* @return void
*/
public function testMapAndBuild() {
$map = Table::map();
$this->assertEquals([], $map);

$options = ['connection' => $this->connection];
Table::map('things', $options);
$map = Table::map();
$this->assertEquals(['things' => $options], $map);
$this->assertEquals($options, Table::map('things'));

$options += ['schema' => ['id' => ['rubbish']]];

$table = Table::build('foo', ['table' => 'things']);
$this->assertInstanceOf('Cake\ORM\Table', $table);
$this->assertEquals('things', $table->table());
$this->assertEquals('foo', $table->alias());
}

public function testFindAllNoFields() {
$this->_createThingsTable();
$table = new Table(['name' => 'things', 'connection' => $this->connection]);
$table = new Table(['table' => 'things', 'connection' => $this->connection]);
$results = $table->find('all')->toArray();
$expected = [
['things' => ['id' => 1, 'title' => 'a title', 'body' => 'a body']],
Expand All @@ -104,7 +127,7 @@ public function testFindAllNoFields() {

public function testFindAllSomeFields() {
$this->_createThingsTable();
$table = new Table(['name' => 'things', 'connection' => $this->connection]);
$table = new Table(['table' => 'things', 'connection' => $this->connection]);
$results = $table->find('all')->select(['id', 'title'])->toArray();
$expected = [
['things' => ['id' => 1, 'title' => 'a title']],
Expand All @@ -122,7 +145,7 @@ public function testFindAllSomeFields() {

public function testFindAllConditionAutoTypes() {
$this->_createDatesTable();
$table = new Table(['name' => 'dates', 'connection' => $this->connection]);
$table = new Table(['table' => 'dates', 'connection' => $this->connection]);
$query = $table->find('all')
->select(['id', 'name'])
->where(['posted >=' => new \DateTime('2012-12-22 12:01')]);
Expand All @@ -138,4 +161,5 @@ public function testFindAllConditionAutoTypes() {
];
$this->assertSame($expected, $query->toArray());
}

}

0 comments on commit 013b246

Please sign in to comment.