From cf2a459056a380e8f723147171377299261268dc Mon Sep 17 00:00:00 2001 From: Luiz Marin <67489841+luizcmarin@users.noreply.github.com> Date: Sat, 23 Nov 2024 20:22:02 -0300 Subject: [PATCH] Update ModelCommand.php Just look, I imagine something like this. But the generated name may seem a bit 'strange'. Lack of creativity, I think. It became 'table.RelatedTable_link_field'. For example, "$this->belongsTo('UsersPermissionGroups_permission_group_id', [...." (*OR should it be the other way around? :) :) ) The reason for adding the field name is illustrated below: $this->belongsTo('Users_Users_inserted_id', [ 'className' => 'Users', 'foreignKey' => 'inserted_id', ]); $this->belongsTo('Users_Users_updated_id', [ 'className' => 'Users', 'foreignKey' => 'updated_id', ]); $this->belongsTo('Users_Users_designated_id', [ 'className' => 'Users', 'foreignKey' => 'designated_id', ]); $this->belongsTo('Users_Users_converted_id', [ 'className' => 'Users', 'foreignKey' => 'converted_id', ]); Well, if the idea is useful, I hope you can refine it and adapt it to cakePHP standards. --- src/Command/ModelCommand.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Command/ModelCommand.php b/src/Command/ModelCommand.php index 423f088e..e87bc4ef 100644 --- a/src/Command/ModelCommand.php +++ b/src/Command/ModelCommand.php @@ -382,6 +382,7 @@ public function findBelongsTo(Table $model, array $associations, ?Arguments $arg } $assoc = [ 'alias' => $tmpModelName, + 'className' => $tmpModelName, 'foreignKey' => $fieldName, ]; if ($schema->getColumn($fieldName)['null'] === false) { @@ -392,6 +393,9 @@ public function findBelongsTo(Table $model, array $associations, ?Arguments $arg if ($this->plugin && empty($assoc['className'])) { $assoc['className'] = $this->plugin . '.' . $assoc['alias']; } + if (!empty($assoc['className'])) { + $assoc['alias'] = $assoc['className'] . '_' . $model->getAlias() . '_' . $fieldName; + } $associations['belongsTo'][] = $assoc; }