From ac33316f6b3ea2daa3ff226f509542356082ad24 Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 26 Nov 2024 10:31:20 +0100 Subject: [PATCH] Make sure custom relations work out with new alias strictness. --- src/Command/ModelCommand.php | 7 ++++++- tests/TestCase/Command/ModelCommandTest.php | 9 ++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Command/ModelCommand.php b/src/Command/ModelCommand.php index 2dcbb680..05f46112 100644 --- a/src/Command/ModelCommand.php +++ b/src/Command/ModelCommand.php @@ -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; } @@ -387,6 +388,9 @@ public function findBelongsTo(Table $model, array $associations, ?Arguments $arg 'alias' => $tmpModelName, 'foreignKey' => $fieldName, ]; + if ($className && $className !== $tmpModelName) { + $assoc['className'] = $className; + } if ($schema->getColumn($fieldName)['null'] === false) { $assoc['joinType'] = 'INNER'; } @@ -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; } diff --git a/tests/TestCase/Command/ModelCommandTest.php b/tests/TestCase/Command/ModelCommandTest.php index dcefa197..4e981b8f 100644 --- a/tests/TestCase/Command/ModelCommandTest.php +++ b/tests/TestCase/Command/ModelCommandTest.php @@ -597,9 +597,10 @@ public function testGetAssociationsConstraints() $expected = [ [ - 'alias' => 'Users', + 'alias' => 'Senders', 'foreignKey' => 'sender_id', 'joinType' => 'INNER', + 'className' => 'Users', ], [ 'alias' => 'Receivers', @@ -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', ], ], ];