Symfony2 doctrine 主从示例

配置slave

# config.yml
doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        slaves:
            slave1:
                host:       "%database_slave1_host%"
                port:       "%database_slave1_port%"
                dbname:     "%database_slave1_name%"
                user:       "%database_slave1_user%"
                password:   "%database_slave1_password%"
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #     path:     "%database_path%"
  
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

存储数据

$manager = $this->get('doctrine.orm.default_entity_manager');
$user = new UserBundleEntityUser();
$user->setUsername('pleamon');
$user->setPassword('tianji');
$manager->persist($user);
$manager->flush();

读取slave数据

$manager = $this->get('doctrine.orm.default_entity_manager');
$user = $manager->getRepository('UserBundle:User')->findOneByUsername('pleamon');

读取master数据

$manager = $this->get('doctrine.orm.default_entity_manager');
$manager->getConnection()->connect('master');
$user = $manager->getRepository('UserBundle:User')->findOneByUsername('pleamon');


by 雪洁 2015-12-09 04:02:42 1995 views
我来说几句

相关文章