Only bake HABTM associations for tables that exist.

bake often guesses wrong when it comes to habtm. Only add associations
we're highly confident of. This includes tables where the foreign table
also exists.

Refs #3532
This commit is contained in:
mark_story 2013-01-15 21:43:30 -05:00
parent eff4004261
commit 0ed9e3c120

View file

@ -621,6 +621,7 @@ class ModelTask extends BakeTask {
public function findHasAndBelongsToMany(Model $model, $associations) { public function findHasAndBelongsToMany(Model $model, $associations) {
$foreignKey = $this->_modelKey($model->name); $foreignKey = $this->_modelKey($model->name);
foreach ($this->_tables as $otherTable) { foreach ($this->_tables as $otherTable) {
$tableName = null;
$tempOtherModel = $this->_getModelObject($this->_modelName($otherTable), $otherTable); $tempOtherModel = $this->_getModelObject($this->_modelName($otherTable), $otherTable);
$modelFieldsTemp = $tempOtherModel->schema(true); $modelFieldsTemp = $tempOtherModel->schema(true);
@ -628,17 +629,12 @@ class ModelTask extends BakeTask {
$otherOffset = strpos($otherTable, '_' . $model->table); $otherOffset = strpos($otherTable, '_' . $model->table);
if ($offset === 0) { if ($offset === 0) {
$offset = strlen($model->table . '_'); $tableName = substr($otherTable, strlen($model->table . '_'));
$habtmName = $this->_modelName(substr($otherTable, $offset));
$associations['hasAndBelongsToMany'][] = array(
'alias' => $habtmName,
'className' => $habtmName,
'foreignKey' => $foreignKey,
'associationForeignKey' => $this->_modelKey($habtmName),
'joinTable' => $otherTable
);
} elseif ($otherOffset === 0) { } 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( $associations['hasAndBelongsToMany'][] = array(
'alias' => $habtmName, 'alias' => $habtmName,
'className' => $habtmName, 'className' => $habtmName,