Skip to content
Permalink
Browse files

Making the model require database connection only wen needed or reque…

…sted
  • Loading branch information...
lorenzo committed Jul 16, 2010
1 parent 6761287 commit a13be623b51c041065b079d5185857299f44ba02
Showing with 18 additions and 7 deletions.
  1. +9 −5 cake/libs/model/model.php
  2. +9 −2 cake/tests/cases/libs/model/model_write.test.php
@@ -462,16 +462,16 @@ function __construct($id = false, $table = null, $ds = null) {
$this->Behaviors = new BehaviorCollection();
if ($this->useTable !== false) {
$this->setDataSource($ds);
if ($this->useTable === null) {
$this->useTable = Inflector::tableize($this->name);
}
$this->setSource($this->useTable);
if ($this->displayField == null) {
$this->displayField = $this->hasField(array('title', 'name', $this->primaryKey));
}
$this->table = $this->useTable;
$this->tableToModel[$this->table] = $this->alias;
} elseif ($this->table === false) {
$this->table = Inflector::tableize($this->name);
}
@@ -785,7 +785,7 @@ function __generateAssociation($type, $assocKey) {
*/
public function setSource($tableName) {
$this->setDataSource($this->useDbConfig);
$db = $this->getDataSource();
$db = ConnectionManager::getDataSource($this->useDbConfig);
$db->cacheSources = ($this->cacheSources && $db->cacheSources);
if ($db->isInterfaceSupported('listSources')) {
@@ -800,7 +800,6 @@ public function setSource($tableName) {
}
$this->table = $this->useTable = $tableName;
$this->tableToModel[$this->table] = $this->alias;
$this->schema();
}
/**
@@ -2790,7 +2789,7 @@ public function setDataSource($dataSource = null) {
if ($dataSource != null) {
$this->useDbConfig = $dataSource;
}
$db = $this->getDataSource();
$db = ConnectionManager::getDataSource($this->useDbConfig);
if (!empty($oldConfig) && isset($db->config['prefix'])) {
$oldDb =& ConnectionManager::getDataSource($oldConfig);
@@ -2813,6 +2812,11 @@ public function setDataSource($dataSource = null) {
* @return object A DataSource object
*/
public function &getDataSource() {
static $configured = false;
if (!$configured && $this->useTable !== false) {
$configured = true;
$this->setSource($this->useTable);
}
return ConnectionManager::getDataSource($this->useDbConfig);
}
@@ -2961,10 +2961,17 @@ function testSaveAllTransactionNoRollback() {
$db = ConnectionManager::create('mock_transaction', array(
'datasource' => 'MockTransactionDbo',
));
$db->expects($this->at(2))
->method('isInterfaceSupported')
->with('describe')
->will($this->returnValue(true));
$db->expects($this->once())
->method('describe')
->will($this->returnValue(array()));
$db->expects($this->once())->method('rollback');
$Post = new Post();
$Post->useDbConfig = 'mock_transaction';
$Post = new Post('mock_transaction');
$Post->validate = array(
'title' => array('rule' => array('notEmpty'))

0 comments on commit a13be62

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