How to use multiply database wit

2019-04-16  本文已影响0人  王晨玮Mark

The key is ManagerRegistry Class. You can read the method inside.

For example. Maybe you can copy the following code to your IDE directly.

IN ADDITION, you must config multiply database info at the configuration file.
How to config

<?php

namespace SessionBundle\Factory;

use Doctrine\DBAL\Connection;
use Symfony\Bridge\Doctrine\ManagerRegistry;

class DbalFactory
{
    /**
     * DB name
     * @var String
     */
    const DEFAULT = 'cAuth';

    /**
     * @var Connection
     */
    private $connection;

    /**
     * DbalFactory constructor
     * @param ManagerRegistry $managerRegistry
     */
    public function __construct(ManagerRegistry $managerRegistry)
    {
        $this->connection = $managerRegistry->getConnection(self::DEFAULT);
    }

    /**
     * get DBconnection
     * @return Connection
     */
    public function get(): Connection
    {
        return $this->connection;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读