Skip to content
Permalink
Browse files

Changing how dynamic "with" models are loaded

Changing all calls to get model's datasourse to use Model::getDatasource()
  • Loading branch information...
lorenzo committed Jul 15, 2010
1 parent 0ec30be commit e165f7d5594292dc6588776ec2bd8c2319a59845
Showing with 44 additions and 45 deletions.
  1. +36 −39 cake/libs/model/model.php
  2. +8 −6 cake/tests/cases/libs/model/model_integration.test.php
@@ -492,8 +492,7 @@ public function __call($method, $params) {
if ($result !== array('unhandled')) {
return $result;
}
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$return = $db->query($method, $params, $this);
$return = $this->getDataSource()->query($method, $params, $this);
return $return;
}
@@ -515,8 +514,10 @@ public function __isset($name) {
if (empty($relation['with'])) {
continue;
}
if (is_array($relation['with']) && key($relation['with']) === $name){
$className = $name;
if (is_array($relation['with'])) {
if (key($relation['with']) === $name) {
$className = $name;
}
} else {
list($plugin, $class) = pluginSplit($relation['with']);
if ($class === $name) {
@@ -525,6 +526,7 @@ public function __isset($name) {
}
if ($className) {
$assocKey = $k;
$dynamic = !empty($relation['dynamicWith']);
break(2);
}
}
@@ -536,7 +538,17 @@ public function __isset($name) {
}
list($plugin, $className) = pluginSplit($className);
$this->__constructLinkedModel($name, $className, $plugin);
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);
}
if (!empty($assocKey)) {
$this->hasAndBelongsToMany[$assocKey]['joinTable'] = $this->{$name}->table;
if (count($this->{$name}->schema()) <= 2 && $this->{$name}->primaryKey !== false) {
@@ -757,22 +769,11 @@ function __generateAssociation($type, $assocKey) {
}
$this->{$type}[$assocKey][$key] = $data;
}
}
if (!empty($this->{$type}[$assocKey]['with'])) {
$joinClass = $this->{$type}[$assocKey]['with'];
if (is_array($joinClass)) {
$joinClass = key($joinClass);
if ($dynamicWith) {
$this->{$type}[$assocKey]['dynamicWith'] = true;
}
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 @@ function __generateAssociation($type, $assocKey) {
*/
public function setSource($tableName) {
$this->setDataSource($this->useDbConfig);
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
$db->cacheSources = ($this->cacheSources && $db->cacheSources);
if ($db->isInterfaceSupported('listSources')) {
@@ -882,7 +883,7 @@ public function deconstruct($field, $data) {
$dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', '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'];
$date = array();
@@ -943,7 +944,7 @@ public function deconstruct($field, $data) {
*/
public function schema($field = false) {
if (!is_array($this->_schema) || $field === true) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
$db->cacheSources = ($this->cacheSources && $db->cacheSources);
if ($db->isInterfaceSupported('describe') && $this->useTable !== false) {
$this->_schema = $db->describe($this, $field);
@@ -985,7 +986,7 @@ public function getColumnTypes() {
* @return string Column type
*/
public function getColumnType($column) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
$cols = $this->schema();
$model = null;
@@ -1272,7 +1273,7 @@ function save($data = null, $validate = true, $fieldList = array()) {
return false;
}
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
foreach ($dateFields as $updateCol) {
if ($this->hasField($updateCol) && !in_array($updateCol, $fields)) {
@@ -1589,7 +1590,7 @@ function saveAll($data = null, $options = array()) {
if (empty($data)) {
$data = $this->data;
}
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
$options = array_merge(array('validate' => 'first', 'atomic' => true), $options);
$this->validationErrors = $validationErrors = array();
@@ -1794,8 +1795,7 @@ function __save($data, $options) {
* @link http://book.cakephp.org/view/1031/Saving-Your-Data
*/
function updateAll($fields, $conditions = true) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
return $db->update($this, $fields, null, $conditions);
return $this->getDataSource()->update($this, $fields, null, $conditions);
}
/**
@@ -1814,7 +1814,7 @@ function delete($id = null, $cascade = true) {
$id = $this->id;
if ($this->exists() && $this->beforeDelete($cascade)) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
$filters = $this->Behaviors->trigger($this, 'beforeDelete', array($cascade), array(
'break' => true, 'breakOn' => false
));
@@ -1924,7 +1924,7 @@ function deleteAll($conditions, $cascade = true, $callbacks = false) {
if (empty($conditions)) {
return false;
}
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
if (!$cascade && !$callbacks) {
return $db->delete($this, $conditions);
@@ -2097,7 +2097,7 @@ function find($conditions = null, $fields = array(), $order = null, $recursive =
}
}
if (!$db =& ConnectionManager::getDataSource($this->useDbConfig)) {
if (!$db = $this->getDataSource()) {
return false;
}
@@ -2153,7 +2153,7 @@ function _findFirst($state, $query, $results = array()) {
*/
function _findCount($state, $query, $results = array()) {
if ($state == 'before') {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
if (empty($query['fields'])) {
$query['fields'] = $db->calculate($this, 'count');
} elseif (is_string($query['fields']) && !preg_match('/count/i', $query['fields'])) {
@@ -2432,7 +2432,7 @@ public function isUnique($fields, $or = true) {
*/
function query() {
$params = func_get_args();
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
return call_user_func_array(array(&$db, 'query'), $params);
}
@@ -2694,7 +2694,7 @@ public function escapeField($field = null, $alias = null) {
if (empty($field)) {
$field = $this->primaryKey;
}
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
if (strpos($field, $db->name($alias) . '.') === 0) {
return $field;
}
@@ -2766,8 +2766,7 @@ public function setInsertID($id) {
* @return int Number of rows
*/
public function getNumRows() {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
return $db->lastNumRows();
return $this->getDataSource()->lastNumRows();
}
/**
@@ -2776,8 +2775,7 @@ public function getNumRows() {
* @return int Number of rows
*/
public function getAffectedRows() {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
return $db->lastAffected();
return $this->getDataSource()->lastAffected();
}
/**
@@ -2792,7 +2790,7 @@ public function setDataSource($dataSource = null) {
if ($dataSource != null) {
$this->useDbConfig = $dataSource;
}
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db = $this->getDataSource();
if (!empty($oldConfig) && isset($db->config['prefix'])) {
$oldDb =& ConnectionManager::getDataSource($oldConfig);
@@ -2815,8 +2813,7 @@ public function setDataSource($dataSource = null) {
* @return object A DataSource object
*/
public function &getDataSource() {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
return $db;
return ConnectionManager::getDataSource($this->useDbConfig);
}
/**
@@ -57,7 +57,7 @@ class ModelIntegrationTest extends BaseModelTest {
* @return void
*/
public function testAssociationLazyLoading() {
$this->loadFixtures('ArticleFeatured', 'User', 'Category', 'Comment', 'ArticleFeaturedsTags');
$this->loadFixtures('ArticleFeatured', 'User', 'Category', 'Comment', 'ArticleFeaturedsTags', 'Tag');
$Article = new ArticleFeatured();
$this->assertTrue(isset($Article->belongsTo['User']));
$this->assertFalse(property_exists($Article, 'User'));
@@ -79,7 +79,8 @@ public function testAssociationLazyLoading() {
$this->assertTrue(property_exists($Article, 'Tag'));
$this->assertTrue(isset($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->assertEquals($Article->hasAndBelongsToMany['Tag']['joinTable'], 'article_featureds_tags');
$this->assertEquals($Article->hasAndBelongsToMany['Tag']['associationForeignKey'], 'tag_id');
@@ -92,22 +93,21 @@ public function testAssociationLazyLoading() {
* @return void
*/
public function testAssociationLazyLoadWithHABTM() {
$this->loadFixtures('Article', 'UuidTag', 'Fruit', 'FruitsUuidTag');
$this->loadFixtures('Article', 'UuidTag', 'Fruit', 'FruitsUuidTag', 'ArticlesTag');
$Article = new ArticleB();
$this->assertTrue(isset($Article->hasAndBelongsToMany['TagB']));
$this->assertFalse(property_exists($Article, 'TagB'));
$this->assertType('TagB', $Article->TagB);
//Dynamic "with" models are not lazy loaded
$this->assertTrue(property_exists($Article, 'ArticlesTag'));
$this->assertFalse(property_exists($Article, 'ArticlesTag'));
$this->assertType('AppModel', $Article->ArticlesTag);
$UuidTag = new UuidTag();
$this->assertTrue(isset($UuidTag->hasAndBelongsToMany['Fruit']));
$this->assertFalse(property_exists($UuidTag, 'Fruit'));
$this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
$this->assertTrue(isset($UuidTag->Fruit));
//But non-dynamic with models are lazy loaded
$this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag'));
$this->assertTrue(isset($UuidTag->FruitsUuidTag));
$this->assertType('FruitsUuidTag', $UuidTag->FruitsUuidTag);
@@ -1199,6 +1199,7 @@ function testAutoConstructAssociations() {
'foreignKey' => false,
'className' => 'AssociationTest2',
'with' => 'JoinAsJoinB',
'dynamicWith' => true,
'associationForeignKey' => 'join_b_id',
'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => ''
@@ -1269,6 +1270,7 @@ function testAutoConstructAssociations() {
'className' => 'Tag',
'joinTable' => 'article_featureds_tags',
'with' => 'ArticleFeaturedsTag',
'dynamicWith' => true,
'foreignKey' => 'article_featured_id',
'associationForeignKey' => 'tag_id',
'conditions' => '',

0 comments on commit e165f7d

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