Skip to content

Commit

Permalink
lazy setup of the datasource.
Browse files Browse the repository at this point in the history
  • Loading branch information
basuke authored and Yosuke Basuke Suzuki committed Sep 7, 2011
1 parent 076df1d commit b14e821
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions cake/libs/model/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,14 @@ class Model extends Object {
*/
protected $_insertID = null;

/**
* Has the datasource been configured.
*
* @var boolean
* @see Model::getDataSource
*/
protected $_sourceConfigured = false;

/**
* List of valid finder method options, supplied as the first parameter to find().
*
Expand Down Expand Up @@ -647,16 +655,16 @@ public 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);
}
Expand Down Expand Up @@ -943,7 +951,6 @@ public function setSource($tableName) {
}
$this->table = $this->useTable = $tableName;
$this->tableToModel[$this->table] = $this->alias;
$this->schema();
}

/**
Expand Down Expand Up @@ -2967,6 +2974,10 @@ public function setDataSource($dataSource = null) {
* @return DataSource A DataSource object
*/
public function getDataSource() {
if (!$this->_sourceConfigured && $this->useTable !== false) {
$this->_sourceConfigured = true;
$this->setSource($this->useTable);
}
return ConnectionManager::getDataSource($this->useDbConfig);
}

Expand Down

0 comments on commit b14e821

Please sign in to comment.