Changing how dynamic "with" models are loaded

Changing all calls to get model's datasourse to use Model::getDatasource()
This commit is contained in:
José Lorenzo Rodríguez Urdaneta 2010-07-14 23:19:38 -04:30
parent 0ec30be076
commit e165f7d559
2 changed files with 44 additions and 45 deletions

View file

@ -492,8 +492,7 @@ class Model extends Object {
if ($result !== array('unhandled')) { if ($result !== array('unhandled')) {
return $result; return $result;
} }
$db =& ConnectionManager::getDataSource($this->useDbConfig); $return = $this->getDataSource()->query($method, $params, $this);
$return = $db->query($method, $params, $this);
return $return; return $return;
} }
@ -515,8 +514,10 @@ class Model extends Object {
if (empty($relation['with'])) { if (empty($relation['with'])) {
continue; continue;
} }
if (is_array($relation['with']) && key($relation['with']) === $name){ if (is_array($relation['with'])) {
if (key($relation['with']) === $name) {
$className = $name; $className = $name;
}
} else { } else {
list($plugin, $class) = pluginSplit($relation['with']); list($plugin, $class) = pluginSplit($relation['with']);
if ($class === $name) { if ($class === $name) {
@ -525,6 +526,7 @@ class Model extends Object {
} }
if ($className) { if ($className) {
$assocKey = $k; $assocKey = $k;
$dynamic = !empty($relation['dynamicWith']);
break(2); break(2);
} }
} }
@ -536,7 +538,17 @@ class Model extends Object {
} }
list($plugin, $className) = pluginSplit($className); list($plugin, $className) = pluginSplit($className);
if (!ClassRegistry::isKeySet($className) && !empty($dynamic)) {
$this->{$className} = new AppModel(array(
'name' => $className,
'table' => $this->hasAndBelongsToMany[$assocKey]['joinTable'],
'ds' => $this->useDbConfig
));
} else {
$this->__constructLinkedModel($name, $className, $plugin); $this->__constructLinkedModel($name, $className, $plugin);
}
if (!empty($assocKey)) { if (!empty($assocKey)) {
$this->hasAndBelongsToMany[$assocKey]['joinTable'] = $this->{$name}->table; $this->hasAndBelongsToMany[$assocKey]['joinTable'] = $this->{$name}->table;
if (count($this->{$name}->schema()) <= 2 && $this->{$name}->primaryKey !== false) { if (count($this->{$name}->schema()) <= 2 && $this->{$name}->primaryKey !== false) {
@ -757,22 +769,11 @@ class Model extends Object {
} }
$this->{$type}[$assocKey][$key] = $data; $this->{$type}[$assocKey][$key] = $data;
} }
if ($dynamicWith) {
$this->{$type}[$assocKey]['dynamicWith'] = true;
} }
if (!empty($this->{$type}[$assocKey]['with'])) {
$joinClass = $this->{$type}[$assocKey]['with'];
if (is_array($joinClass)) {
$joinClass = key($joinClass);
}
list($plugin, $joinClass) = pluginSplit($joinClass);
if (!ClassRegistry::isKeySet($joinClass) && $dynamicWith === true) {
$this->{$joinClass} = new AppModel(array(
'name' => $joinClass,
'table' => $this->{$type}[$assocKey]['joinTable'],
'ds' => $this->useDbConfig
));
}
} }
} }
@ -784,7 +785,7 @@ class Model extends Object {
*/ */
public function setSource($tableName) { public function setSource($tableName) {
$this->setDataSource($this->useDbConfig); $this->setDataSource($this->useDbConfig);
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
$db->cacheSources = ($this->cacheSources && $db->cacheSources); $db->cacheSources = ($this->cacheSources && $db->cacheSources);
if ($db->isInterfaceSupported('listSources')) { if ($db->isInterfaceSupported('listSources')) {
@ -882,7 +883,7 @@ class Model extends Object {
$dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec'); $dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec');
$timeFields = array('H' => 'hour', 'i' => 'min', 's' => 'sec'); $timeFields = array('H' => 'hour', 'i' => 'min', 's' => 'sec');
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
$format = $db->columns[$type]['format']; $format = $db->columns[$type]['format'];
$date = array(); $date = array();
@ -943,7 +944,7 @@ class Model extends Object {
*/ */
public function schema($field = false) { public function schema($field = false) {
if (!is_array($this->_schema) || $field === true) { if (!is_array($this->_schema) || $field === true) {
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
$db->cacheSources = ($this->cacheSources && $db->cacheSources); $db->cacheSources = ($this->cacheSources && $db->cacheSources);
if ($db->isInterfaceSupported('describe') && $this->useTable !== false) { if ($db->isInterfaceSupported('describe') && $this->useTable !== false) {
$this->_schema = $db->describe($this, $field); $this->_schema = $db->describe($this, $field);
@ -985,7 +986,7 @@ class Model extends Object {
* @return string Column type * @return string Column type
*/ */
public function getColumnType($column) { public function getColumnType($column) {
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
$cols = $this->schema(); $cols = $this->schema();
$model = null; $model = null;
@ -1272,7 +1273,7 @@ class Model extends Object {
return false; return false;
} }
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
foreach ($dateFields as $updateCol) { foreach ($dateFields as $updateCol) {
if ($this->hasField($updateCol) && !in_array($updateCol, $fields)) { if ($this->hasField($updateCol) && !in_array($updateCol, $fields)) {
@ -1589,7 +1590,7 @@ class Model extends Object {
if (empty($data)) { if (empty($data)) {
$data = $this->data; $data = $this->data;
} }
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
$options = array_merge(array('validate' => 'first', 'atomic' => true), $options); $options = array_merge(array('validate' => 'first', 'atomic' => true), $options);
$this->validationErrors = $validationErrors = array(); $this->validationErrors = $validationErrors = array();
@ -1794,8 +1795,7 @@ class Model extends Object {
* @link http://book.cakephp.org/view/1031/Saving-Your-Data * @link http://book.cakephp.org/view/1031/Saving-Your-Data
*/ */
function updateAll($fields, $conditions = true) { function updateAll($fields, $conditions = true) {
$db =& ConnectionManager::getDataSource($this->useDbConfig); return $this->getDataSource()->update($this, $fields, null, $conditions);
return $db->update($this, $fields, null, $conditions);
} }
/** /**
@ -1814,7 +1814,7 @@ class Model extends Object {
$id = $this->id; $id = $this->id;
if ($this->exists() && $this->beforeDelete($cascade)) { if ($this->exists() && $this->beforeDelete($cascade)) {
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
$filters = $this->Behaviors->trigger($this, 'beforeDelete', array($cascade), array( $filters = $this->Behaviors->trigger($this, 'beforeDelete', array($cascade), array(
'break' => true, 'breakOn' => false 'break' => true, 'breakOn' => false
)); ));
@ -1924,7 +1924,7 @@ class Model extends Object {
if (empty($conditions)) { if (empty($conditions)) {
return false; return false;
} }
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
if (!$cascade && !$callbacks) { if (!$cascade && !$callbacks) {
return $db->delete($this, $conditions); return $db->delete($this, $conditions);
@ -2097,7 +2097,7 @@ class Model extends Object {
} }
} }
if (!$db =& ConnectionManager::getDataSource($this->useDbConfig)) { if (!$db = $this->getDataSource()) {
return false; return false;
} }
@ -2153,7 +2153,7 @@ class Model extends Object {
*/ */
function _findCount($state, $query, $results = array()) { function _findCount($state, $query, $results = array()) {
if ($state == 'before') { if ($state == 'before') {
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
if (empty($query['fields'])) { if (empty($query['fields'])) {
$query['fields'] = $db->calculate($this, 'count'); $query['fields'] = $db->calculate($this, 'count');
} elseif (is_string($query['fields']) && !preg_match('/count/i', $query['fields'])) { } elseif (is_string($query['fields']) && !preg_match('/count/i', $query['fields'])) {
@ -2432,7 +2432,7 @@ class Model extends Object {
*/ */
function query() { function query() {
$params = func_get_args(); $params = func_get_args();
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
return call_user_func_array(array(&$db, 'query'), $params); return call_user_func_array(array(&$db, 'query'), $params);
} }
@ -2694,7 +2694,7 @@ class Model extends Object {
if (empty($field)) { if (empty($field)) {
$field = $this->primaryKey; $field = $this->primaryKey;
} }
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
if (strpos($field, $db->name($alias) . '.') === 0) { if (strpos($field, $db->name($alias) . '.') === 0) {
return $field; return $field;
} }
@ -2766,8 +2766,7 @@ class Model extends Object {
* @return int Number of rows * @return int Number of rows
*/ */
public function getNumRows() { public function getNumRows() {
$db =& ConnectionManager::getDataSource($this->useDbConfig); return $this->getDataSource()->lastNumRows();
return $db->lastNumRows();
} }
/** /**
@ -2776,8 +2775,7 @@ class Model extends Object {
* @return int Number of rows * @return int Number of rows
*/ */
public function getAffectedRows() { public function getAffectedRows() {
$db =& ConnectionManager::getDataSource($this->useDbConfig); return $this->getDataSource()->lastAffected();
return $db->lastAffected();
} }
/** /**
@ -2792,7 +2790,7 @@ class Model extends Object {
if ($dataSource != null) { if ($dataSource != null) {
$this->useDbConfig = $dataSource; $this->useDbConfig = $dataSource;
} }
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db = $this->getDataSource();
if (!empty($oldConfig) && isset($db->config['prefix'])) { if (!empty($oldConfig) && isset($db->config['prefix'])) {
$oldDb =& ConnectionManager::getDataSource($oldConfig); $oldDb =& ConnectionManager::getDataSource($oldConfig);
@ -2815,8 +2813,7 @@ class Model extends Object {
* @return object A DataSource object * @return object A DataSource object
*/ */
public function &getDataSource() { public function &getDataSource() {
$db =& ConnectionManager::getDataSource($this->useDbConfig); return ConnectionManager::getDataSource($this->useDbConfig);
return $db;
} }
/** /**

View file

@ -57,7 +57,7 @@ class ModelIntegrationTest extends BaseModelTest {
* @return void * @return void
*/ */
public function testAssociationLazyLoading() { public function testAssociationLazyLoading() {
$this->loadFixtures('ArticleFeatured', 'User', 'Category', 'Comment', 'ArticleFeaturedsTags'); $this->loadFixtures('ArticleFeatured', 'User', 'Category', 'Comment', 'ArticleFeaturedsTags', 'Tag');
$Article = new ArticleFeatured(); $Article = new ArticleFeatured();
$this->assertTrue(isset($Article->belongsTo['User'])); $this->assertTrue(isset($Article->belongsTo['User']));
$this->assertFalse(property_exists($Article, 'User')); $this->assertFalse(property_exists($Article, 'User'));
@ -79,7 +79,8 @@ class ModelIntegrationTest extends BaseModelTest {
$this->assertTrue(property_exists($Article, 'Tag')); $this->assertTrue(property_exists($Article, 'Tag'));
$this->assertTrue(isset($Article->Tag)); $this->assertTrue(isset($Article->Tag));
$this->assertType('Tag', $Article->Tag); $this->assertType('Tag', $Article->Tag);
$this->assertTrue(property_exists($Article, 'ArticleFeaturedsTag'));
$this->assertFalse(property_exists($Article, 'ArticleFeaturedsTag'));
$this->assertType('AppModel', $Article->ArticleFeaturedsTag); $this->assertType('AppModel', $Article->ArticleFeaturedsTag);
$this->assertEquals($Article->hasAndBelongsToMany['Tag']['joinTable'], 'article_featureds_tags'); $this->assertEquals($Article->hasAndBelongsToMany['Tag']['joinTable'], 'article_featureds_tags');
$this->assertEquals($Article->hasAndBelongsToMany['Tag']['associationForeignKey'], 'tag_id'); $this->assertEquals($Article->hasAndBelongsToMany['Tag']['associationForeignKey'], 'tag_id');
@ -92,14 +93,13 @@ class ModelIntegrationTest extends BaseModelTest {
* @return void * @return void
*/ */
public function testAssociationLazyLoadWithHABTM() { public function testAssociationLazyLoadWithHABTM() {
$this->loadFixtures('Article', 'UuidTag', 'Fruit', 'FruitsUuidTag'); $this->loadFixtures('Article', 'UuidTag', 'Fruit', 'FruitsUuidTag', 'ArticlesTag');
$Article = new ArticleB(); $Article = new ArticleB();
$this->assertTrue(isset($Article->hasAndBelongsToMany['TagB'])); $this->assertTrue(isset($Article->hasAndBelongsToMany['TagB']));
$this->assertFalse(property_exists($Article, 'TagB')); $this->assertFalse(property_exists($Article, 'TagB'));
$this->assertType('TagB', $Article->TagB); $this->assertType('TagB', $Article->TagB);
//Dynamic "with" models are not lazy loaded $this->assertFalse(property_exists($Article, 'ArticlesTag'));
$this->assertTrue(property_exists($Article, 'ArticlesTag'));
$this->assertType('AppModel', $Article->ArticlesTag); $this->assertType('AppModel', $Article->ArticlesTag);
$UuidTag = new UuidTag(); $UuidTag = new UuidTag();
@ -107,7 +107,7 @@ class ModelIntegrationTest extends BaseModelTest {
$this->assertFalse(property_exists($UuidTag, 'Fruit')); $this->assertFalse(property_exists($UuidTag, 'Fruit'));
$this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag')); $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
$this->assertTrue(isset($UuidTag->Fruit)); $this->assertTrue(isset($UuidTag->Fruit));
//But non-dynamic with models are lazy loaded
$this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag')); $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
$this->assertTrue(isset($UuidTag->FruitsUuidTag)); $this->assertTrue(isset($UuidTag->FruitsUuidTag));
$this->assertType('FruitsUuidTag', $UuidTag->FruitsUuidTag); $this->assertType('FruitsUuidTag', $UuidTag->FruitsUuidTag);
@ -1199,6 +1199,7 @@ class ModelIntegrationTest extends BaseModelTest {
'foreignKey' => false, 'foreignKey' => false,
'className' => 'AssociationTest2', 'className' => 'AssociationTest2',
'with' => 'JoinAsJoinB', 'with' => 'JoinAsJoinB',
'dynamicWith' => true,
'associationForeignKey' => 'join_b_id', 'associationForeignKey' => 'join_b_id',
'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => '' 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => ''
@ -1269,6 +1270,7 @@ class ModelIntegrationTest extends BaseModelTest {
'className' => 'Tag', 'className' => 'Tag',
'joinTable' => 'article_featureds_tags', 'joinTable' => 'article_featureds_tags',
'with' => 'ArticleFeaturedsTag', 'with' => 'ArticleFeaturedsTag',
'dynamicWith' => true,
'foreignKey' => 'article_featured_id', 'foreignKey' => 'article_featured_id',
'associationForeignKey' => 'tag_id', 'associationForeignKey' => 'tag_id',
'conditions' => '', 'conditions' => '',