I’m in the process of taking a site from EE2 > EE5. I have the local version running on 3.5.17 with no problems. I am currently updating custom EE2 modules to work in EE3. That was going fine until I got to this module that needs to connect to a secondary database for a forum.
Using https://docs.expressionengine.com/latest/development/legacy/database/connecting-to-an-external-database.html I have made the additions to addon.setup.php and the module file as noted in the docs. I’ve tried it with my own database references, and now with everything exactly as shown in the docs, and consistently, I get this error:
Exception Caught
Dependency Injection: Unregistered service "help_desk:db"
ee/EllisLab/ExpressionEngine/Service/Dependency/InjectionContainer.php:184
Stack Trace: hide details
#0 ee/EllisLab/ExpressionEngine/Boot/boot.php(132): EllisLab\ExpressionEngine\Service\Dependency\InjectionContainer->make('help_desk:db')
#1 user/addons/augi_screen_name_changes/mod.augi_screen_name_changes.php(108): ee('help_desk:db')
#2 user/addons/augi_screen_name_changes/mcp.augi_screen_name_changes.php(183): Augi_screen_name_changes->process_change('25542', 'approve')
#3 [internal function]: Augi_screen_name_changes_mcp->approve('25542')
#4 ee/EllisLab/ExpressionEngine/Controller/Addons/Addons.php(1660): call_user_func_array(Array, Array)
#5 ee/EllisLab/ExpressionEngine/Controller/Addons/Addons.php(920): EllisLab\ExpressionEngine\Controller\Addons\Addons->getModuleSettings('augi_screen_nam...', 'approve', Array)
#6 [internal function]: EllisLab\ExpressionEngine\Controller\Addons\Addons->settings('augi_screen_nam...', 'approve', '25542')
#7 ee/EllisLab/ExpressionEngine/Core/Core.php(189): call_user_func_array(Array, Array)
#8 ee/EllisLab/ExpressionEngine/Core/Core.php(94): EllisLab\ExpressionEngine\Core\Core->runController(Array)
#9 ee/EllisLab/ExpressionEngine/Boot/boot.php(151): EllisLab\ExpressionEngine\Core\Core->run(Object(EllisLab\ExpressionEngine\Core\Request))
#10 public_html/admin/index.php(143): require_once('...')
#10 public_html/admin/index.php(143): require_once('...')
The module is installed and working, other than calling this function via a link on the Control Panel page.
Here is my addon.setup.php file:
<?php
/**
* AUGI Screen Name Changes config file
*
* @package Augi_screen_name_changes
* @author Chad Crowell <[email protected]>
* @link https://helloswish.com
* @copyright Copyright (c) 2019, Swish Digital
*/
return array(
'author' => 'Chad Crowell',
'author_url' => 'https://helloswish.com',
'name' => 'AUGI Screen Name Changes',
'description' => 'Review and approve or decline screen name change requests.',
'version' => '3.0.0',
'namespace' => 'Swish\AugiScreenNameChanges',
'settings_exist' => TRUE,
'services' => array(
// This service will be used to query our external database
// e.g., ee('help_desk:db')->select()
'db' => function($addon)
{
return $addon->make('help_desk:Database')->newQuery();
},
// This service manages our external database connection
// e.g., ee('help_desk:Database')->getLog()
'Database' => function($addon)
{
// Makes sure we only do this work once per page request
static $db;
if (empty($db))
{
// fetch config from system/user/config/help_desk_database.php
$config = ee('Config')->getFile('help_desk_database');
// create the DBConfig object
$db_config = new Database\DBConfig($config);
// select the database connection group
$db_config->getGroupConfig('help_desk');
// connect to and make the Database object
$db = new Database\Database($db_config);
}
return $db;
}
)
);
Here is my config/help_desk_database.php file (which I know the addon.setup.php file is seeing by var_dumping $config at the top of that file):
<?php
$config['database'] = array(
'help_desk' => array(
'hostname' => 'localhost',
'username' => 'db_username',
'password' => 'db_password',
'database' => 'forum_db_name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE
),
);
Here is the code calling the secondary db in my module mod.xxx.php file:
ee('help_desk:db')
->where('userid', $user_id)
->update('user', $user_record);
As noted above, I have tried this with my own db reference names (forum:db) instead of the example (help_desk:db), and also with the database config being loaded out of the EE master config, as well as the recommended manner where it loads from a discreet config file, and no matter what, I get this error.
Ideas? Thanks.
The error is more to do with trying to access an unregistered service and not so much to do with database connections.help_desk
is just an example add-on name and is supposed to be replaced with your add-on’s short name, i.e. the folder name. Looks like that might be augi_screen_name_changes
? If so, try replacing help_desk:db
with augi_screen_name_changes:db
and help_desk:Database
with `augi_screen_name_changes:Database. Here’s more about defining services:
https://docs.expressionengine.com/latest/development/addon_setup_php_file.html#services
Thanks Kevin. I do realize help_desk is an example, and I had gotten so frustrated using my own naming that I tried it with the exact example code from the docs, which is the code I posted.
So, this database I need to access, I need to access from several different add-ons… are you saying that I can’t name it consistently across the various add-ons so that I can call it the same way across those add ons?
So, in this add on it would be:
ee('augi_screen_name_changes:db')
->where('userid', $user_id)
->update('user', $user_record);
and in another add-on that accesses the same db it might be:
ee('some_other_addon_name:db')
->where('id', $id)
->update('table', $data);
Is there a way to define this db connection globally so I can use ee(‘forum:db’) throughout several add-ons for best consistency?
Thanks again Kevin, with the info you provided, I was able to take a different approach. Instead of trying to register the service in the specific module I was building, I setup a new plugin with a dummy plugin class that serves to simply register the external db as a service. Once I did that and installed it, I was able to use the external db reference in the original plugin to communicate with the external db, so the service does appear to be registered globally.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.