For some reason, when I use containable with a join table in a plugin, Cake uses the default datasource instead of the custom one. I've been all over stackoverflow and Google, and tried everything I could find to no avail.
I'm trying to create a plugin that uses its own datasource:
plugins/Vorien/NPCData/config/app.php:
return [
'Datasources' => [
'npcdata' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'npcdata',
'password' => '********',
'database' => 'npcdata',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => true,
'url' => env('DATABASE_URL', null),
],
],
];
plugins/Vorien/NPCData/config/bootstrap.php:
try {
Configure::config('npcdata', new PhpConfig(Plugin::path('Vorien/NPCData') . 'config/'));
Configure::load('app', 'npcdata');
ConnectionManager::config(Configure::consume('Datasources'));
} catch (\Exception $e) {
exit($e->getMessage() . "\n");
}
I used bake to create everything with the parameters:
-p Vorien/NPCData -c npcdata
My join tables look like:
$this->belongsTo('Personas', [
'foreignKey' => 'persona_id',
'className' => 'Vorien/NPCData.Personas'
]);
$this->belongsTo('Archetypes', [
'foreignKey' => 'archetype_id',
'className' => 'Vorien/NPCData.Archetypes'
]);
And have:
public static function defaultConnectionName()
{
return 'npcdata';
}
Running a hasOne or hasMany:
$persona = $this->Personas->get($id, [
'contain' => ['People']
]);
When I try to run anything with a HABTM:
$persona = $this->Personas->get($id, [
'contain' => ['Archetypes']
]);
I get: Error: SQLSTATE[HY000] [1045] Access denied for user 'my_app'@'localhost' (using password: YES) with the Auto-Tables error for PersonasArchetypes
I dug around a lot until I realized that the key was 'my_app'@'localhost' which meant that even with the defaultConnectionName set in PersonasArchetypesTable Cake was using the 'default' connection only for join tables. When I set the default connection in config/app.php everything works. Of course that won't work for a plugin.
Note: The join table isn't alphabetical but that was changed to optional in 3.0. I did try renaming the join tables anyway.
Has anyone else run into this? Could it be a bug? Or am I just missing something?
EDIT: I've also tried adding the database name before the table in all three Table files of the HABTM. (database.table instead of just table)
Plugin::load('npcdata', ['bootstrap' => true]);