Friday, August 29, 2014

Re: 3.0.x - defaultConnectionName not working in Plugin

How did you load your plugin?

On Friday, August 29, 2014 9:32:48 AM UTC+2, Joe Theuerkauf wrote:
A while ago i created this thread: https://groups.google.com/forum/#!topic/cake-php/A5WHkiWCPd4

i was trying to hook up tables from a second database, using a different Datasource connection from app.php.

My default is "kodiak", the other is "websites". It's a general-use set of tables, like MimeTypes, Captcha questions, etc.

i got them working by putting them in together with Table/Entity classes from the "kodiak" database. However, i wanted to organize the class files for these Tables/Entities away from the other stuff, and the suggestion was to use a Plugin syntax.

Cut to tonight:

1. i used the cake command line to build models within my plugin:

cake bake model Thook\Websites.Captchas --connection=websites

2. The files were built successfully within Plugin/Thook/Websites/src.

3. i added
public static function defaultConnectionName() {
    return 'websites';
}

to each of the Table classes i built, as i had done originally under App/Model/Table. i kinda feel this part should be added automatically if --connection is specified on the command line, but the point is moot.

4. i changed the line in my Controller that loads the Captchas Model (per http://book.cakephp.org/3.0/en/plugins.html#plugin-models at the end of that section):
$captchasTable = TableRegistry::get('Thook/Websites.Captchas');

i think that's the right syntax. If i try anything else similar to it, i get class-not-found errors.

Using that syntax, i get this:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'kodiak.captchas' doesn't exist
Error: An Internal Error Has Occurred.
Stack Trace
    ROOT\vendor\cakephp\cakephp\src\Database\Schema\Collection.php line 116 → Cake\Database\Schema\Collection->_reflect(string, string, array, Cake\Database\Schema\Table)
    ROOT\vendor\cakephp\cakephp\src\ORM\Table.php line 323 → Cake\Database\Schema\Collection->describe(string)
    ROOT\vendor\cakephp\cakephp\src\ORM\Query.php line 136 → Cake\ORM\Table->schema()
    ROOT\vendor\cakephp\cakephp\src\ORM\Query.php line 119 → Cake\ORM\Query->addDefaultTypes(Cake\ORM\Table)
    ROOT\vendor\cakephp\cakephp\src\ORM\Table.php line 927 → Cake\ORM\Query->__construct(Cake\Database\Connection, Cake\ORM\Table)
    ROOT\vendor\cakephp\cakephp\src\ORM\Table.php line 724 → Cake\ORM\Table->query()
    APP/Controller\ContactsController.php line 65 → Cake\ORM\Table->find(string)
(expanded...)
         $termsTable = TableRegistry::get('Terms');
         $captchasTable = TableRegistry::get('Thook/Websites.Captchas');
Line 65: $captcha = $captchasTable->find('random')->first();

So it brings me back to the same thing i was struggling with last time: it looks like the Table class gets loaded, but defaultConnectionName isn't doing its job to tell the App to use the 'websites' connection instead of 'default' (kodiak).

find('random') is in a Table extension class i added, and was also working until i started trying the Plugin approach for the Model.

Any help?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: