Skip to content

Commit

Permalink
Fix issues with model bake.
Browse files Browse the repository at this point in the history
Setting the classname on associations requires the fully namespaced
classname, not just a short alias. Exclude the short alias form of
a classname from the association creation.
  • Loading branch information
markstory committed Mar 20, 2014
1 parent a8cb4d6 commit 30590ba
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 20 deletions.
9 changes: 2 additions & 7 deletions src/Console/Command/Task/ModelTask.php
Expand Up @@ -130,8 +130,8 @@ public function generate($name) {
);
$this->bakeTable($model, $data);
$this->bakeEntity($model, $data);
$this->bakeFixture($model, $table);
$this->bakeTest($model);
$this->bakeFixture($model->alias(), $table);
$this->bakeTest($model->alias());
}

/**
Expand Down Expand Up @@ -219,13 +219,11 @@ public function findBelongsTo($model, $associations) {
$tmpModelName = $this->_modelNameFromKey($fieldName);
$associations['belongsTo'][] = [
'alias' => $tmpModelName,
'className' => $tmpModelName,
'foreignKey' => $fieldName,
];
} elseif ($fieldName === 'parent_id') {
$associations['belongsTo'][] = [
'alias' => 'Parent' . $model->alias(),
'className' => $model->alias(),
'foreignKey' => $fieldName,
];
}
Expand Down Expand Up @@ -262,13 +260,11 @@ public function findHasMany($model, $associations) {
if (!in_array($fieldName, $primaryKey) && $fieldName == $foreignKey) {
$assoc = [
'alias' => $otherModel->alias(),
'className' => $otherModel->alias(),
'foreignKey' => $fieldName
];
} elseif ($otherTable == $tableName && $fieldName === 'parent_id') {
$assoc = [
'alias' => 'Child' . $model->alias(),
'className' => $model->alias(),
'foreignKey' => $fieldName
];
}
Expand Down Expand Up @@ -308,7 +304,6 @@ public function findBelongsToMany($model, $associations) {
$habtmName = $this->_modelName($assocTable);
$associations['belongsToMany'][] = [
'alias' => $habtmName,
'className' => $habtmName,
'foreignKey' => $foreignKey,
'targetForeignKey' => $this->_modelKey($habtmName),
'joinTable' => $otherTable
Expand Down
13 changes: 0 additions & 13 deletions tests/TestCase/Console/Command/Task/ModelTaskTest.php
Expand Up @@ -168,21 +168,18 @@ public function testGetAssociations() {
'belongsTo' => [
[
'alias' => 'BakeUsers',
'className' => 'BakeUsers',
'foreignKey' => 'bake_user_id',
],
],
'hasMany' => [
[
'alias' => 'BakeComments',
'className' => 'BakeComments',
'foreignKey' => 'bake_article_id',
],
],
'belongsToMany' => [
[
'alias' => 'BakeTags',
'className' => 'BakeTags',
'foreignKey' => 'bake_article_id',
'joinTable' => 'bake_articles_bake_tags',
'targetForeignKey' => 'bake_tag_id',
Expand All @@ -204,12 +201,10 @@ public function testBelongsToGeneration() {
'belongsTo' => [
[
'alias' => 'BakeArticles',
'className' => 'BakeArticles',
'foreignKey' => 'bake_article_id',
],
[
'alias' => 'BakeUsers',
'className' => 'BakeUsers',
'foreignKey' => 'bake_user_id',
],
]
Expand All @@ -222,7 +217,6 @@ public function testBelongsToGeneration() {
'belongsTo' => [
[
'alias' => 'ParentCategoryThreads',
'className' => 'CategoryThreads',
'foreignKey' => 'parent_id',
],
]
Expand All @@ -243,7 +237,6 @@ public function testHasManyGeneration() {
'hasMany' => [
[
'alias' => 'BakeComments',
'className' => 'BakeComments',
'foreignKey' => 'bake_article_id',
],
],
Expand All @@ -256,7 +249,6 @@ public function testHasManyGeneration() {
'hasMany' => [
[
'alias' => 'ChildCategoryThreads',
'className' => 'CategoryThreads',
'foreignKey' => 'parent_id',
],
]
Expand All @@ -277,7 +269,6 @@ public function testHasAndBelongsToManyGeneration() {
'belongsToMany' => [
[
'alias' => 'BakeTags',
'className' => 'BakeTags',
'foreignKey' => 'bake_article_id',
'joinTable' => 'bake_articles_bake_tags',
'targetForeignKey' => 'bake_tag_id',
Expand Down Expand Up @@ -576,26 +567,22 @@ public function testBakeTableRelations() {
'belongsTo' => [
[
'alias' => 'SomethingElse',
'className' => 'SomethingElse',
'foreignKey' => 'something_else_id',
],
[
'alias' => 'BakeUser',
'className' => 'BakeUser',
'foreignKey' => 'bake_user_id',
],
],
'hasMany' => [
[
'alias' => 'BakeComment',
'className' => 'BakeComment',
'foreignKey' => 'parent_id',
],
],
'belongsToMany' => [
[
'alias' => 'BakeTag',
'className' => 'BakeTag',
'foreignKey' => 'bake_article_id',
'joinTable' => 'bake_articles_bake_tags',
'targetForeignKey' => 'bake_tag_id',
Expand Down

0 comments on commit 30590ba

Please sign in to comment.