Permalink
Browse files

Fixing autoFields causing invalid SQL when cross database joins are b…

…eing done. Tests added. Fixes #476
  • Loading branch information...
1 parent 5ed5c54 commit a5f019799ae1adbfdae7f511e6404efc132750aa @markstory markstory committed Apr 20, 2010
@@ -185,23 +185,32 @@ 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;
}
}
}
}
}
+
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']);
@@ -3574,6 +3574,39 @@ function testResetMultipleHabtmAssociations() {
$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
*
* @param mixed $Model

0 comments on commit a5f0197

Please sign in to comment.