craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.25k stars 629 forks source link

[4.x]: config/db.php enableLogging missing/gone? #12198

Closed benediktblank closed 1 year ago

benediktblank commented 1 year ago

What happened?

Description

enableLogging is still mentioned here: https://craftcms.com/docs/4.x/upgrade.html#logging

It's linking to this page: https://craftcms.com/docs/4.x/config/db.html#enablelogging

But there's nothing there. And when I add that config, I get a fatal error.

Steps to reproduce

  1. Add 'enableLogging' => true to config/db.php
  2. Get a fatal error

Expected behavior

The config that is documented works

Actual behavior

Craft crashes

Craft CMS version

4.2.3

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

-

brandonkelly commented 1 year ago

Sorry, this was a docs bug. You can control DB logging by setting the yii\db\Connection::$enableLogging property directly in config/app.php:

return [
    'components' => [
        'db' => function() {
            $config = craft\helpers\App::dbConfig();

            // Always enable DB logging (by default it would only be enabled for Dev Mode)
            $config['enableLogging'] = true;

            return Craft::createObject($config);
        },
    ],
];
benediktblank commented 1 year ago

@brandonkelly

Wait, so I gotta move my complete DB config from db.php to app.php for this to work?

When I just add what you provided I still get an error saying: "The configuration for the "db" component must contain a "class" element."

When I provide that like that:

'components' => [
        'db' => [
            'class' => '\yii\db\Connection',
            'enableLogging' => App::env('ENVIRONMENT') === 'dev'
        ]

it wants dsn element and so on: "Connection::dsn cannot be empty."

There is no mention about in the 4.X db documentation either: https://craftcms.com/docs/4.x/config/db.html

brandonkelly commented 1 year ago

You don’t need to make any changes to your DB configuration in config/db.php (besides removing the enableLogging setting that shouldn’t have been added there in the first place).

brandonkelly commented 1 year ago

And sorry, my original db component config example was bad. You should be setting it to a closure that calls craft\helpers\App::dbConfig(), modifies the resulting config, and then returns an object created from it. I’ve updated the example above.

No need to override the class property. (Craft actually uses its own connection class that extends yii\db\Connection, and class will already be set within the $config array returned by craft\helpers\App::dbConfig().)

ericdrosas87 commented 8 months ago

The docs still show the incorrect information, can they be updated with a working example?

brandonkelly commented 8 months ago

@ericdrosas87 What is incorrect?

ericdrosas87 commented 8 months ago

Screenshot 2024-01-13 at 9 07 46 AM

@brandonkelly The documentation for v4.X still shows that the all that needs to be added to app.php are the two key-values. Which still produces the Exception 'yii\base\InvalidConfigException' with message 'The configuration for the "db" component must contain a "class" element.' error message. According to your response above though an anonymous function needs to be passed in instead?

brandonkelly commented 8 months ago

@ericdrosas87 Doh, thanks! Fixed now: https://craftcms.com/docs/4.x/upgrade.html#logging

ericdrosas87 commented 8 months ago

@ericdrosas87 Doh, thanks! Fixed now: https://craftcms.com/docs/4.x/upgrade.html#logging

Excellent - thank you for updating that!