Skip to content

Commit 30590ba

Browse files
committed
Fix issues with model bake.
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.
1 parent a8cb4d6 commit 30590ba

File tree

2 files changed

+2
-20
lines changed

2 files changed

+2
-20
lines changed

src/Console/Command/Task/ModelTask.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public function generate($name) {
130130
);
131131
$this->bakeTable($model, $data);
132132
$this->bakeEntity($model, $data);
133-
$this->bakeFixture($model, $table);
134-
$this->bakeTest($model);
133+
$this->bakeFixture($model->alias(), $table);
134+
$this->bakeTest($model->alias());
135135
}
136136

137137
/**
@@ -219,13 +219,11 @@ public function findBelongsTo($model, $associations) {
219219
$tmpModelName = $this->_modelNameFromKey($fieldName);
220220
$associations['belongsTo'][] = [
221221
'alias' => $tmpModelName,
222-
'className' => $tmpModelName,
223222
'foreignKey' => $fieldName,
224223
];
225224
} elseif ($fieldName === 'parent_id') {
226225
$associations['belongsTo'][] = [
227226
'alias' => 'Parent' . $model->alias(),
228-
'className' => $model->alias(),
229227
'foreignKey' => $fieldName,
230228
];
231229
}
@@ -262,13 +260,11 @@ public function findHasMany($model, $associations) {
262260
if (!in_array($fieldName, $primaryKey) && $fieldName == $foreignKey) {
263261
$assoc = [
264262
'alias' => $otherModel->alias(),
265-
'className' => $otherModel->alias(),
266263
'foreignKey' => $fieldName
267264
];
268265
} elseif ($otherTable == $tableName && $fieldName === 'parent_id') {
269266
$assoc = [
270267
'alias' => 'Child' . $model->alias(),
271-
'className' => $model->alias(),
272268
'foreignKey' => $fieldName
273269
];
274270
}
@@ -308,7 +304,6 @@ public function findBelongsToMany($model, $associations) {
308304
$habtmName = $this->_modelName($assocTable);
309305
$associations['belongsToMany'][] = [
310306
'alias' => $habtmName,
311-
'className' => $habtmName,
312307
'foreignKey' => $foreignKey,
313308
'targetForeignKey' => $this->_modelKey($habtmName),
314309
'joinTable' => $otherTable

tests/TestCase/Console/Command/Task/ModelTaskTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,18 @@ public function testGetAssociations() {
168168
'belongsTo' => [
169169
[
170170
'alias' => 'BakeUsers',
171-
'className' => 'BakeUsers',
172171
'foreignKey' => 'bake_user_id',
173172
],
174173
],
175174
'hasMany' => [
176175
[
177176
'alias' => 'BakeComments',
178-
'className' => 'BakeComments',
179177
'foreignKey' => 'bake_article_id',
180178
],
181179
],
182180
'belongsToMany' => [
183181
[
184182
'alias' => 'BakeTags',
185-
'className' => 'BakeTags',
186183
'foreignKey' => 'bake_article_id',
187184
'joinTable' => 'bake_articles_bake_tags',
188185
'targetForeignKey' => 'bake_tag_id',
@@ -204,12 +201,10 @@ public function testBelongsToGeneration() {
204201
'belongsTo' => [
205202
[
206203
'alias' => 'BakeArticles',
207-
'className' => 'BakeArticles',
208204
'foreignKey' => 'bake_article_id',
209205
],
210206
[
211207
'alias' => 'BakeUsers',
212-
'className' => 'BakeUsers',
213208
'foreignKey' => 'bake_user_id',
214209
],
215210
]
@@ -222,7 +217,6 @@ public function testBelongsToGeneration() {
222217
'belongsTo' => [
223218
[
224219
'alias' => 'ParentCategoryThreads',
225-
'className' => 'CategoryThreads',
226220
'foreignKey' => 'parent_id',
227221
],
228222
]
@@ -243,7 +237,6 @@ public function testHasManyGeneration() {
243237
'hasMany' => [
244238
[
245239
'alias' => 'BakeComments',
246-
'className' => 'BakeComments',
247240
'foreignKey' => 'bake_article_id',
248241
],
249242
],
@@ -256,7 +249,6 @@ public function testHasManyGeneration() {
256249
'hasMany' => [
257250
[
258251
'alias' => 'ChildCategoryThreads',
259-
'className' => 'CategoryThreads',
260252
'foreignKey' => 'parent_id',
261253
],
262254
]
@@ -277,7 +269,6 @@ public function testHasAndBelongsToManyGeneration() {
277269
'belongsToMany' => [
278270
[
279271
'alias' => 'BakeTags',
280-
'className' => 'BakeTags',
281272
'foreignKey' => 'bake_article_id',
282273
'joinTable' => 'bake_articles_bake_tags',
283274
'targetForeignKey' => 'bake_tag_id',
@@ -576,26 +567,22 @@ public function testBakeTableRelations() {
576567
'belongsTo' => [
577568
[
578569
'alias' => 'SomethingElse',
579-
'className' => 'SomethingElse',
580570
'foreignKey' => 'something_else_id',
581571
],
582572
[
583573
'alias' => 'BakeUser',
584-
'className' => 'BakeUser',
585574
'foreignKey' => 'bake_user_id',
586575
],
587576
],
588577
'hasMany' => [
589578
[
590579
'alias' => 'BakeComment',
591-
'className' => 'BakeComment',
592580
'foreignKey' => 'parent_id',
593581
],
594582
],
595583
'belongsToMany' => [
596584
[
597585
'alias' => 'BakeTag',
598-
'className' => 'BakeTag',
599586
'foreignKey' => 'bake_article_id',
600587
'joinTable' => 'bake_articles_bake_tags',
601588
'targetForeignKey' => 'bake_tag_id',

0 commit comments

Comments
 (0)