Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow saving habtm between db-schemas. Fixes #105

DboSource::buildJoinStatement compares its own database against the
corresponding join Model database.  This is required for habtm cross-db
find() operation to work.

Model::save uses joined Model's datasource instead of its own when
saving HABTM relationship.
  • Loading branch information...
commit 2de793df8b3e25152546fc825944fadf3481f5d0 1 parent f0ca6e4
Rachman Chavik authored
Showing with 22 additions and 2 deletions.
  1. +10 −1 lib/Cake/Model/Datasource/DboSource.php
  2. +12 −1 lib/Cake/Model/Model.php
11 lib/Cake/Model/Datasource/DboSource.php
View
@@ -1623,7 +1623,16 @@ public function buildJoinStatement($join) {
$data['conditions'] = trim($this->conditions($data['conditions'], true, false));
}
if (!empty($data['table'])) {
- $data['table'] = $this->fullTableName($data['table']);
+ if ($data['table'] instanceof Model) {
+ $databaseName = $data['table']->getDataSource()->config['database'];
+ if ($this->config['database'] == $databaseName) {
+ $data['table'] = $this->fullTableName($data['table']);
+ } else {
+ $data['table'] = $databaseName .'.'. $this->fullTableName($data['table']);
+ }
+ } else {
+ $data['table'] = $this->fullTableName($data['table']);
+ }
}
return $this->renderJoinStatement($data);
}
13 lib/Cake/Model/Model.php
View
@@ -1639,7 +1639,18 @@ public function save($data = null, $validate = true, $fieldList = array()) {
}
if (!empty($joined) && $success === true) {
- $this->_saveMulti($joined, $this->id, $db);
+ $joinModel = is_array($joined) ? key($joined) : false;
+ if ($joinModel && !empty($this->hasAndBelongsToMany[$joinModel]['with'])) {
+ if (is_array($this->hasAndBelongsToMany[$joinModel]['with'])) {
+ $habtmModel = key($this->hasAndBelongsToMany[$joinModel]['with']);
+ } else {
+ $habtmModel = $this->hasAndBelongsToMany[$joinModel]['with'];
+ }
+ $dbHabtm = $this->{$habtmModel}->getDataSource();
+ $this->_saveMulti($joined, $this->id, $dbHabtm);
+ } else {
+ $this->_saveMulti($joined, $this->id, $db);
+ }
}
if ($success && $count > 0) {
Please sign in to comment.
Something went wrong with that request. Please try again.