diff --git a/lib/Cake/Console/Command/Task/ModelTask.php b/lib/Cake/Console/Command/Task/ModelTask.php index be90d2168c3..f5479a27930 100644 --- a/lib/Cake/Console/Command/Task/ModelTask.php +++ b/lib/Cake/Console/Command/Task/ModelTask.php @@ -621,6 +621,7 @@ public function findHasOneAndMany(Model $model, $associations) { public function findHasAndBelongsToMany(Model $model, $associations) { $foreignKey = $this->_modelKey($model->name); foreach ($this->_tables as $otherTable) { + $tableName = null; $tempOtherModel = $this->_getModelObject($this->_modelName($otherTable), $otherTable); $modelFieldsTemp = $tempOtherModel->schema(true); @@ -628,17 +629,12 @@ public function findHasAndBelongsToMany(Model $model, $associations) { $otherOffset = strpos($otherTable, '_' . $model->table); if ($offset === 0) { - $offset = strlen($model->table . '_'); - $habtmName = $this->_modelName(substr($otherTable, $offset)); - $associations['hasAndBelongsToMany'][] = array( - 'alias' => $habtmName, - 'className' => $habtmName, - 'foreignKey' => $foreignKey, - 'associationForeignKey' => $this->_modelKey($habtmName), - 'joinTable' => $otherTable - ); + $tableName = substr($otherTable, strlen($model->table . '_')); } elseif ($otherOffset === 0) { - $habtmName = $this->_modelName(substr($otherTable, 0, $otherOffset)); + $tableName = substr($otherTable, 0, $otherOffset); + } + if ($tableName && in_array($tableName, $this->_tables)) { + $habtmName = $this->_modelName($tableName); $associations['hasAndBelongsToMany'][] = array( 'alias' => $habtmName, 'className' => $habtmName,