0

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)

1
  • Did you set the bootstrap option to true while configuring your plugin in config/bootstrap.php? Like this : Plugin::load('npcdata', ['bootstrap' => true]); Commented Jun 23, 2016 at 7:57

1 Answer 1

0

There was a bug when using the table name for the joinTables parameter in a plugin where it wasn't setting the connection from the table object.

Using the plugin prefixed table object in either the joinTables or the through options worked fine.

There's a patch #9018 with plans for a more robust fix in 3.2.12.

Many thanks to the Cake folks for all their help. And patience.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.