Skip to content
Permalink
Browse files

Only generate associations when _id occurs at the end of a field.

If `_id` is mid column it should not result in a generated association.

Fixes #4503
  • Loading branch information...
markstory committed Sep 8, 2014
1 parent 669e6d7 commit 7f310612dd2a0d0361c05d81ae5b71e512a800ab
Showing with 26 additions and 12 deletions.
  1. +11 −12 src/Shell/Task/ModelTask.php
  2. +15 −0 tests/TestCase/Shell/Task/ModelTaskTest.php
@@ -221,25 +221,24 @@ public function findBelongsTo($model, array $associations) {
$schema = $model->schema();
$primary = (array)$schema->primaryKey();
foreach ($schema->columns() as $fieldName) {
$offset = strpos($fieldName, '_id');
if (!preg_match('/^.*_id$/', $fieldName)) {
continue;
}
$assoc = false;
if ($fieldName !== 'parent_id' && $offset !== false) {
$tmpModelName = $this->_modelNameFromKey($fieldName);
$assoc = [
'alias' => $tmpModelName,
'foreignKey' => $fieldName
];
} elseif ($fieldName === 'parent_id') {
if ($fieldName === 'parent_id') {
$className = ($this->plugin) ? $this->plugin . '.' . $model->alias() : $model->alias();
$assoc = [
'alias' => 'Parent' . $model->alias(),
'className' => $className,
'foreignKey' => $fieldName
];
}
if ($assoc === false) {
continue;
} else {
$tmpModelName = $this->_modelNameFromKey($fieldName);
$assoc = [
'alias' => $tmpModelName,
'foreignKey' => $fieldName
];
}
if ($this->plugin && empty($assoc['className'])) {
@@ -376,6 +376,21 @@ public function testBelongsToGenerationCompositeKey() {
$this->assertEquals($expected, $result);
}
/**
* Test that belongsTo generation ignores _id mid-column
*
* @return void
*/
public function testBelongsToGenerationIdMidColumn() {
$model = TableRegistry::get('Articles');
$model->schema([
'id' => ['type' => 'integer'],
'thing_id_field' => ['type' => 'integer'],
]);
$result = $this->Task->findBelongsTo($model, []);
$this->assertEquals([], $result);
}
/**
* test that hasOne and/or hasMany relations are generated properly.
*

0 comments on commit 7f31061

Please sign in to comment.
You can’t perform that action at this time.