Skip to content

Commit

Permalink
Only bake HABTM associations for tables that exist.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markstory committed Jan 16, 2013
1 parent eff4004 commit 0ed9e3c
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/Cake/Console/Command/Task/ModelTask.php
Expand Up @@ -621,24 +621,20 @@ 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);

$offset = strpos($otherTable, $model->table . '_');
$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,
Expand Down

1 comment on commit 0ed9e3c

@renatorib
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.