Setting ConnectionProvider from active-record in symfony

Hello!

I installed symfony 7.3 and installed yiisoft/active-record extension.
I want to set ConnectionProvider::set($connection), but i am not sure when it’s better place.

I created ActiveRecordBundle:

<?php
declare(strict_types=1);

namespace App\Bundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Yiisoft\ActiveRecord\ConnectionProvider;
use Yiisoft\Db\Connection\ConnectionInterface;

final class ActiveRecordBundle extends Bundle
{
    public function boot(): void
    {
        ConnectionProvider::set($this->container->get(ConnectionInterface::class));
    }
}

And then i registered bundle in bundles.php:

<?php

use App\Bundle\ActiveRecordBundle;

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
    Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
    Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
    Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
    Symfony\UX\StimulusBundle\StimulusBundle::class => ['all' => true],
    Symfony\UX\Turbo\TurboBundle::class => ['all' => true],
    Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
    Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
    Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
    Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
    ActiveRecordBundle::class => ['all' => true],
];

It works. Could you tell me - is it correct way for it?

Looks like it’s fine.

1 Like