Skip to content

Commit

Permalink
Make sure custom relations work out with new alias strictness.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 26, 2024
1 parent 9631dd0 commit ac33316
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,9 @@ public function findBelongsTo(Table $model, array $associations, ?Arguments $arg
) {
$allowAliasRelations = $args && $args->getOption('skip-relation-check');
$found = $this->findTableReferencedBy($schema, $fieldName);
$className = null;
if ($found) {
$tmpModelName = Inflector::camelize($found);
$className = ($this->plugin ? $this->plugin . '.' : '') . Inflector::camelize($found);
} elseif (!$allowAliasRelations) {
continue;
}
Expand All @@ -387,6 +388,9 @@ public function findBelongsTo(Table $model, array $associations, ?Arguments $arg
'alias' => $tmpModelName,
'foreignKey' => $fieldName,
];
if ($className && $className !== $tmpModelName) {

Check failure on line 391 in src/Command/ModelCommand.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Variable $className might not be defined.
$assoc['className'] = $className;
}
if ($schema->getColumn($fieldName)['null'] === false) {
$assoc['joinType'] = 'INNER';
}
Expand All @@ -395,6 +399,7 @@ public function findBelongsTo(Table $model, array $associations, ?Arguments $arg
if ($this->plugin && empty($assoc['className'])) {
$assoc['className'] = $this->plugin . '.' . $assoc['alias'];
}

$associations['belongsTo'][] = $assoc;
}

Expand Down
9 changes: 6 additions & 3 deletions tests/TestCase/Command/ModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,10 @@ public function testGetAssociationsConstraints()

$expected = [
[
'alias' => 'Users',
'alias' => 'Senders',
'foreignKey' => 'sender_id',
'joinType' => 'INNER',
'className' => 'Users',
],
[
'alias' => 'Receivers',
Expand Down Expand Up @@ -675,14 +676,16 @@ public function testBelongsToGenerationConstraints()
$expected = [
'belongsTo' => [
[
'alias' => 'Users',
'alias' => 'Senders',
'foreignKey' => 'sender_id',
'joinType' => 'INNER',
'className' => 'Users',
],
[
'alias' => 'Users',
'alias' => 'Receivers',
'foreignKey' => 'receiver_id',
'joinType' => 'INNER',
'className' => 'Users',
],
],
];
Expand Down

0 comments on commit ac33316

Please sign in to comment.