diff --git a/cake/libs/model/behaviors/containable.php b/cake/libs/model/behaviors/containable.php index 083bdc6cb6a..3d5efcce28f 100644 --- a/cake/libs/model/behaviors/containable.php +++ b/cake/libs/model/behaviors/containable.php @@ -185,7 +185,7 @@ function beforeFind(&$Model, $query) { foreach (array('hasOne', 'belongsTo') as $type) { if (!empty($Model->{$type})) { foreach ($Model->{$type} as $assoc => $data) { - if (!empty($data['fields'])) { + if ($Model->useDbConfig == $Model->{$assoc}->useDbConfig && !empty($data['fields'])) { foreach ((array) $data['fields'] as $field) { $query['fields'][] = (strpos($field, '.') === false ? $assoc . '.' : '') . $field; } @@ -193,15 +193,24 @@ function beforeFind(&$Model, $query) { } } } + if (!empty($mandatory[$Model->alias])) { foreach ($mandatory[$Model->alias] as $field) { if ($field == '--primaryKey--') { $field = $Model->primaryKey; } else if (preg_match('/^.+\.\-\-[^-]+\-\-$/', $field)) { list($modelName, $field) = explode('.', $field); - $field = $modelName . '.' . (($field === '--primaryKey--') ? $Model->$modelName->primaryKey : $field); + if ($Model->useDbConfig == $Model->{$modelName}->useDbConfig) { + $field = $modelName . '.' . ( + ($field === '--primaryKey--') ? $Model->$modelName->primaryKey : $field + ); + } else { + $field = null; + } + } + if ($field !== null) { + $query['fields'][] = $field; } - $query['fields'][] = $field; } } $query['fields'] = array_unique($query['fields']); diff --git a/cake/tests/cases/libs/model/behaviors/containable.test.php b/cake/tests/cases/libs/model/behaviors/containable.test.php index 239578ef14d..69f4b63e343 100644 --- a/cake/tests/cases/libs/model/behaviors/containable.test.php +++ b/cake/tests/cases/libs/model/behaviors/containable.test.php @@ -3573,6 +3573,39 @@ function testResetMultipleHabtmAssociations() { $this->Article->find('all', array('contain' => array('ShortTag' => array('fields' => array('ShortTag.tag', 'ShortTag.created'))))); $this->assertEqual($expected, $this->Article->hasAndBelongsToMany); } +/** + * test that autoFields doesn't splice in fields from other databases. + * + * @return void + */ + function testAutoFieldsWithMultipleDatabases() { + $config = new DATABASE_CONFIG(); + + $skip = $this->skipIf( + !isset($config->test) || !isset($config->test2), + '%s Primary and secondary test databases not configured, skipping cross-database ' + .'join tests.' + .' To run these tests, you must define $test and $test2 in your database configuration.' + ); + if ($skip) { + return; + } + + $db =& ConnectionManager::getDataSource('test2'); + $this->_fixtures[$this->_fixtureClassMap['User']]->create($db); + $this->_fixtures[$this->_fixtureClassMap['User']]->insert($db); + + $this->Article->User->setDataSource('test2'); + + $result = $this->Article->find('all', array( + 'fields' => array('Article.title'), + 'contain' => array('User') + )); + $this->assertTrue(isset($result[0]['Article'])); + $this->assertTrue(isset($result[0]['User'])); + + $this->_fixtures[$this->_fixtureClassMap['User']]->drop($db); + } /** * containments method *