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

Pls remove string type in parameter $group at Database::forge #1605

Closed
Martin-4Spaces opened this issue Dec 11, 2018 · 4 comments
Closed

Pls remove string type in parameter $group at Database::forge #1605

Martin-4Spaces opened this issue Dec 11, 2018 · 4 comments

Comments

@Martin-4Spaces
Copy link

It is possible to provide a group array to Database::connect(). The same should be possible to Database::forge and Database::utils.

@lonnieezell
Copy link
Member

while I think it's a fine change to make, I'm curious why you're using just an array and not adding it as a group in the database config. What type of app are you making? Trying to develop a better picture of other people's use cases.

@Martin-4Spaces
Copy link
Author

@lonnieezell
I am importing data from an old database supplied by a customer. A one time thing, but the name of that database can change from customer to customer.
I know the database hostname, username and password. I copy the group from database config and only change the database name.
This could be avoided if I could supply database name as second parameter to Database::connect/forge/utils.

@lonnieezell
Copy link
Member

Gotcha. thanks for explaining. Another potential solution is use the .env file to set the database configs, but that would require a PHP/server restart after every change, I think, which isn't feasible if you're doing it all on one server.

@Martin-4Spaces
Copy link
Author

For now, I solved it by adding the forge function to application/Config/Database and added support for group as an array.

    public static function forge($group = null) {
        if (is_array($group)) {
            $config = $group;
            $group  = 'custom-' . md5(json_encode($config));
        }

        $config = new \Config\Database();

        static::ensureFactory();

        if (empty($group))
        {
            $group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
        }

        if (is_string($group) && ! isset($config->$group) && strpos($group, 'custom-') !== 0)
        {
            throw new \InvalidArgumentException($group . ' is not a valid database connection group.');
        }

        if (! isset(static::$instances[$group]))
        {
            $db = static::connect($group);
        }
        else
        {
            $db = static::$instances[$group];
        }

        return static::$factory->loadForge($db);
    }

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

2 participants