我正在研究设置Doctrine 2以使用MySQL的主/从连接的不同方式。设置将是这样,即存在一个具有多个从属服务器的主数据库。所有SELECT语句都应来自随机 Activity 的从属,并且任何UPDATE,INSERT,DELETE语句都应始终委派给主连接。

是否有人以这种方式设置了Doctine 2或对如何使用它有任何想法?

最佳答案

Doctrine2现在在\Doctrine\DBAL\Connections命名空间中具有MasterSlaveConnection。

编辑:
除非这部分不起作用,否则请不要在下面阅读

不再需要重载器,dbal配置将自己获取从属设备。例如

connections:
            default:
                driver:   %database_driver%
                host:     %database_host%
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                slaves:
                    slave1:
                        host:     %database_slave1%
                        dbname:   %database_name%
                        user:     %database_user%
                        password: %database_password%

如果上述方法无效,请尝试以下方法

很简单,只需在每个主机之间放置管道(|)
default:
                driver:   %database_driver%
                host:     %database_host%|%database_slave%|%database_slave2%
                port:     3306
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                wrapper_class: \Foo\Bar\Symfony\Doctrine\Connections\MasterSlave


<?php
namespace Foo\Bar\Symfony\Doctrine\Connections;
use \Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\Connection,
        Doctrine\DBAL\Driver,
        Doctrine\DBAL\Configuration,
        Doctrine\Common\EventManager,
        Doctrine\DBAL\Event\ConnectionEventArgs,
        Doctrine\DBAL\Events,
        Doctrine\DBAL\Cache\QueryCacheProfile;

class MasterSlave extends MasterSlaveConnection
{
        public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null)
        {
                $tempParams = array(
                                'master' => array()
                                , 'slaves' => array()
                                , 'driver' => $params['driver']
                                );
                $hosts = explode('|', $params['host']);
                unset($params['host']);
                foreach($hosts as $num => $host)
                {
                        $params['host'] = $host;
                        if($num == 0)
                        {
                                $tempParams['master'] = $params;
                        }
                        else
                        {
                                $tempParams['slaves'][] = $params;
                        }
                }

                if(!isset($tempParams['master']['driver']))
                        $tempParams['master']['driver'] = "pdo_mysql";

                foreach($tempParams['slaves'] as $k => $slave)
                {
                        if(!isset($slave['driver']))
                                $tempParams['slaves'][$k]['driver'] = "pdo_mysql";
                }
                parent::__construct($tempParams, $driver, $config, $eventManager);
        }

        public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
        {
                try
                {
                        return parent::executeQuery($query, $params, $types, $qcp);
                }
                catch(\Exception $e)
                {
                        $logger = new \Uelib\Core\Logger();
                        $message = $e->getMessage() . "\nSql: " . $query . "\nParams: \n" . print_r($params, true);
                        $logger->log($message);
                        throw $e;
                }
        }
}

关于doctrine-orm - 教义2主/从连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4922723/

10-14 11:32