Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database cannot be autoload #8

Open
iddemirjs opened this issue Nov 4, 2021 · 4 comments
Open

Database cannot be autoload #8

iddemirjs opened this issue Nov 4, 2021 · 4 comments

Comments

@iddemirjs
Copy link

When I added to database in to autoload file. Dot env is not working. How can I solve that?

@anthony-n-brown
Copy link

Seeing the exact same thing on CI3.

It looks like the database helper will be loaded before the .env files are processed which causes any env() calls in the database.php to fail.

@anthony-n-brown
Copy link

anthony-n-brown commented Nov 8, 2021

In system\core\Loader.php, they prioritize loading of the database before any other libraries:

		// Load libraries
		if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
		{
			// Load the database driver.
			if (in_array('database', $autoload['libraries']))
			{
				$this->database();
				$autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
			}

			// Load all other libraries
			$this->library($autoload['libraries']);
		}

If you don't mind touching the Code Igniter code, you can workaround this issue by forcing env to be loaded before database like:

		// Load libraries
		if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
		{
			// Load the environment driver before the database code
			if (in_array('env', $autoload['libraries']))
			{
				$this->library('env');
				$autoload['libraries'] = array_diff($autoload['libraries'], array('env'));
			}
			// Load the database driver.
			if (in_array('database', $autoload['libraries']))
			{
				$this->database();
				$autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
			}

			// Load all other libraries
			$this->library($autoload['libraries']);
		}

@setsunafjustin00208
Copy link

Thanks for the guide :)

@harshadduasane
Copy link

Thanks @anthony-n-brown it works :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants