如何在symfony中,在第二个数据库中创建新实体

时间:2018-05-10 09:31:59

标签: symfony doctrine

我有问题。我能够配置symfony连接到两个数据库。

config.yml:

# Doctrine Configuration
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_mysql
                host:     '%database_host%'
                port:     '%database_port%'
                dbname:   '%database_name%'
                user:     '%database_user%'
                password: '%database_password%'
                charset:  UTF8
            customer:
                driver:   pdo_mysql
                host:     '%database_host2%'
                port:     '%database_port2%'
                dbname:   '%database_name2%'
                user:     '%database_user2%'
                password: '%database_password2%'
                charset:  UTF8

    orm:
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    AppBundle:  ~

            customer:
                connection: customer
                mappings:
                    AppBundle:  ~

在这里我的问题出现了。 如何在第二个数据库中创建新实体?

1 个答案:

答案 0 :(得分:0)

你可以把它做成一个控制器:

$customerEntityManager = $this->getDoctrine()->getManager('customer');

或者这个:

$customerEm = $this->get('doctrine.orm.customer_entity_manager');

然后:

$yourEntity = new YourEntity();
$customerEm->persist(yourEntity);
$customerEm->flush();