Skip to content

Commit

Permalink
Use plugin prefixes when generating models in prefixes.
Browse files Browse the repository at this point in the history
After a bit of discussion in #4026 it sounded baking associations that
default to using the plugin will result in less work instead of
defaulting to app models.
  • Loading branch information
markstory committed Jul 21, 2014
1 parent c09e2b9 commit a3e45b3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Console/Command/Task/ModelTask.php
Expand Up @@ -224,20 +224,30 @@ public function findBelongsTo($model, array $associations) {
$primary = (array)$schema->primaryKey();
foreach ($schema->columns() as $fieldName) {
$offset = strpos($fieldName, '_id');
$assoc = false;
if (!in_array($fieldName, $primary) && $fieldName !== 'parent_id' && $offset !== false) {
$tmpModelName = $this->_modelNameFromKey($fieldName);
$associations['belongsTo'][] = [
$assoc = [
'alias' => $tmpModelName,
'foreignKey' => $fieldName
];
} elseif ($fieldName === 'parent_id') {
$className = ($this->plugin) ? $this->plugin . '.' . $model->alias() : $model->alias();
$associations['belongsTo'][] = [
$assoc = [
'alias' => 'Parent' . $model->alias(),
'className' => $className,
'foreignKey' => $fieldName
];
}

if ($assoc === false) {
continue;
}

if ($this->plugin && empty($assoc['className'])) {
$assoc['className'] = $this->plugin . '.' . $assoc['alias'];
}
$associations['belongsTo'][] = $assoc;
}
return $associations;
}
Expand Down Expand Up @@ -281,6 +291,9 @@ public function findHasMany($model, array $associations) {
'foreignKey' => $fieldName
];
}
if ($assoc && $this->plugin && empty($assoc['className'])) {
$assoc['className'] = $this->plugin . '.' . $assoc['alias'];
}
if ($assoc) {
$associations['hasMany'][] = $assoc;
}
Expand Down Expand Up @@ -314,12 +327,16 @@ public function findBelongsToMany($model, array $associations) {
}
if ($assocTable && in_array($assocTable, $tables)) {
$habtmName = $this->_modelName($assocTable);
$associations['belongsToMany'][] = [
$assoc = [
'alias' => $habtmName,
'foreignKey' => $foreignKey,
'targetForeignKey' => $this->_modelKey($habtmName),
'joinTable' => $otherTable
];
if ($assoc && $this->plugin) {
$assoc['className'] = $this->plugin . '.' . $assoc['alias'];
}
$associations['belongsToMany'][] = $assoc;
}
}
return $associations;
Expand Down
38 changes: 38 additions & 0 deletions tests/TestCase/Console/Command/Task/ModelTaskTest.php
Expand Up @@ -264,6 +264,44 @@ public function testGetAssociations() {
$this->assertEquals($expected, $result);
}

/**
* Test getAssociations in a plugin
*
* @return void
*/
public function testGetAssociationsPlugin() {
$articles = TableRegistry::get('BakeArticles');
$this->Task->plugin = 'TestPlugin';

$result = $this->Task->getAssociations($articles);
$expected = [
'belongsTo' => [
[
'alias' => 'BakeUsers',
'className' => 'TestPlugin.BakeUsers',
'foreignKey' => 'bake_user_id'
],
],
'hasMany' => [
[
'alias' => 'BakeComments',
'className' => 'TestPlugin.BakeComments',
'foreignKey' => 'bake_article_id',
],
],
'belongsToMany' => [
[
'alias' => 'BakeTags',
'className' => 'TestPlugin.BakeTags',
'foreignKey' => 'bake_article_id',
'joinTable' => 'bake_articles_bake_tags',
'targetForeignKey' => 'bake_tag_id',
],
],
];
$this->assertEquals($expected, $result);
}

/**
* test that belongsTo generation works.
*
Expand Down

0 comments on commit a3e45b3

Please sign in to comment.