Lazy-loading the $tablePrefix property in models, Fixes #2277

This commit is contained in:
Jose Lorenzo Rodriguez 2011-12-08 00:10:22 -04:30
parent bc8ae11fc1
commit 73aeb6ba62
2 changed files with 30 additions and 0 deletions

View file

@ -701,6 +701,11 @@ class Model extends Object {
} elseif ($this->table === false) {
$this->table = Inflector::tableize($this->name);
}
if ($this->tablePrefix === null) {
unset($this->tablePrefix);
}
$this->_createLinks();
$this->Behaviors->init($this->alias, $this->actsAs);
}
@ -800,6 +805,13 @@ class Model extends Object {
if ($name === 'displayField') {
return $this->displayField = $this->hasField(array('title', 'name', $this->primaryKey));
}
if ($name === 'tablePrefix') {
$this->setDataSource();
if (property_exists($this, 'tablePrefix')) {
return $this->tablePrefix;
}
return $this->tablePrefix = null;
}
if (isset($this->{$name})) {
return $this->{$name};
}

View file

@ -2049,6 +2049,7 @@ class ModelIntegrationTest extends BaseModelTest {
$result = $TestModel->escapeField('DomainHandle', 'Domain');
$expected = $db->name('Domain.DomainHandle');
$this->assertEquals($expected, $result);
ConnectionManager::drop('mock');
}
/**
@ -2073,4 +2074,21 @@ class ModelIntegrationTest extends BaseModelTest {
$this->assertTrue($Article->hasMethod('pass'));
$this->assertFalse($Article->hasMethod('fail'));
}
/**
* Tests that tablePrefix is taken from the datasource if none is defined in the model
*
* @return void
* @see http://cakephp.lighthouseapp.com/projects/42648/tickets/2277-caketestmodels-in-test-cases-do-not-set-model-tableprefix
*/
public function testModelPrefixFromDatasource() {
ConnectionManager::create('mock', array(
'datasource' => 'DboMock',
'prefix' => 'custom_prefix_'
));
$Article = new Article(false, null, 'mock');
$this->assertEquals('custom_prefix_', $Article->tablePrefix);
ConnectionManager::drop('mock');
}
}