Skip to content

One-time execution when connected to a database #16090

Answered by niden
s-ohnishi asked this question in Q&A
Discussion options

You must be logged in to vote

You can do it when registering your database provider.

$container->setShared(
    'db',
    function () {
        $options = [
            'host'     => envValue('DATA_API_MYSQL_HOST', 'localhost'),
            'username' => envValue('DATA_API_MYSQL_USER', 'phalcon'),
            'password' => envValue('DATA_API_MYSQL_PASS', ''),
            'dbname'   => envValue('DATA_API_MYSQL_NAME', 'phalcon_api'),
        ];

        $connection = new Mysql($options);
        // Set everything to UTF8
        $connection->execute('SET NAMES utf8mb4', []);
        // Set time to UTC
        $connection->execute("SET time_zone='+9:00'", []);

        return $connection;
    }
);

Replies: 1 comment 1 reply

Comment options

niden
Sep 11, 2022
Maintainer Sponsor

You must be logged in to vote
1 reply
@s-ohnishi
Comment options

Answer selected by s-ohnishi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants