Skip to content

\Config\Database::connect returns unexpected result for custom config values #1533

Description

@hwiesmann

name: Bug report
about: \Config\Database::connect returns unexpected result for custom config values


Describe the bug
I am not sure whether this is a bug in the code or if a special remark is missing in the documentation for this kind of constellation:

$myConfiguration1 = [...];
$myConfiguration2 = [...]; // $myConfiguration1 != $myConfiguration2
$db1 = \Config\Database::connect($myConfiguration1);
$db2 = \Config\Database::connect($myConfiguration2);

The astonishing result is: $db1 == $db2!

The reason for this surprise is that internally an explicit configuration gets the tag 'custom' (see Config.php connect()):

		if (is_array($group))
		{
			$config = $group;
			$group = 'custom';
		}

And as $getShared has the default value TRUE the first database is returned for the second call as well (because an instance with the key 'custom' already exists for the first connection).

If this is a bug in the code there are at least two (quick) solutions:

  1. implement a counter so that $group is something like 'custom_1', 'custom_2' etc.
  2. set $getShared to false to prevent returning the wrong instance (this has the consequence that only the last connection instance is internally stored!)

If this is intended behaviour a note in the documentation is required, IMHO!

CodeIgniter 4 version
CodeIgniter 4.0.0 Alpha 2

Affected module(s)
system/Database/Config.php

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugVerified issues on the current code behavior or pull requests that will fix themin progress

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions