From 5d1b04fb0816f24a7b413d16488ce7c0ac8c464d Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Fri, 12 Sep 2025 22:15:17 +0300 Subject: [PATCH] Fix Server issue --- app/src/Lib/Traits/PluggableModelTrait.php | 32 ++++++++++++++++++---- app/src/Model/Table/ServersTable.php | 5 +--- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/src/Lib/Traits/PluggableModelTrait.php b/app/src/Lib/Traits/PluggableModelTrait.php index ec4cd3234..93620388a 100644 --- a/app/src/Lib/Traits/PluggableModelTrait.php +++ b/app/src/Lib/Traits/PluggableModelTrait.php @@ -197,15 +197,35 @@ protected function setPluginRelations() { continue; } + // Derive association alias from "Plugin.Model" + [$pluginName, $modelAlias] = explode('.', $m->plugin, 2); + + // Skip if alias would clash with this table alias or already exists + if ($modelAlias === $this->getAlias()) { + // Avoid "Association alias 'X' is already set." + $this->llog('debug', "Skipping association for plugin '{$m->plugin}' due to alias clash with table alias '{$this->getAlias()}'"); + continue; + } + + if ($this->associations()->has($modelAlias)) { + // Association already defined elsewhere; don't rebind + $this->llog('debug', "Association '{$modelAlias}' already exists, skipping plugin relation '{$m->plugin}'"); + continue; + } + // In general, a model with a "plugin" field has a 1-1 relation // with the instantiated plugin configuration. eg: One instance // of a Server has exactly one SqlServer associated with it. - $this->hasOne($m->plugin) - ->setDependent(true) - ->setCascadeCallbacks(true); - - // Cache the list of entry points that we found - $this->_pluginModels[] = $m->plugin; + // Bind by alias and explicitly set the className. + $this->hasOne($modelAlias) + ->setClassName($m->plugin) + ->setDependent(true) + ->setCascadeCallbacks(true); + + // Cache the list of entry points that we found (avoid duplicates) + if (!in_array($m->plugin, $this->_pluginModels, true)) { + $this->_pluginModels[] = $m->plugin; + } } // isArtifactTable() might not be the exact right test here... diff --git a/app/src/Model/Table/ServersTable.php b/app/src/Model/Table/ServersTable.php index d766ff225..a626f4681 100644 --- a/app/src/Model/Table/ServersTable.php +++ b/app/src/Model/Table/ServersTable.php @@ -72,10 +72,7 @@ public function initialize(array $config): void { $this->hasMany('Pipelines') ->setForeignKey('match_server_id'); -// XXX Note this will bind to (eg) CoreServer but not (eg) SqlProvisioner -// As of Cake 5 this is causing errors with the Servers alias already being set, -// it's not clear that we need this anymore? - // $this->setPluginRelations(); + $this->setPluginRelations(); $this->setDisplayField('description');