From 73aeb6ba62736e4c516b7c38cd33dd2e880157db Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 8 Dec 2011 00:10:22 -0430 Subject: [PATCH] Lazy-loading the $tablePrefix property in models, Fixes #2277 --- lib/Cake/Model/Model.php | 12 ++++++++++++ .../Test/Case/Model/ModelIntegrationTest.php | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index c814d9cd4..3ee5dbba2 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -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}; } diff --git a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php index d57f13616..890156e48 100644 --- a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php +++ b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php @@ -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'); + } + }