在Symfony2中创建表之前,请选择DB Schema

时间:2015-11-03 09:04:24

标签: php symfony doctrine-orm doctrine

在我的项目中,我必须分开数据库,我设置如下:

// app/config/parameters.yml
parameters:
#base
database_host: 127.0.0.1
database_port: null
database_name: nm_admin
database_user: nm_admin
database_password: 'nm_admin'
#user1
database_host_user1: 127.0.0.1
database_port_user1: null
database_name_user1: nm_user1
database_user_user1: nm_user1
database_password_user1: 'nm_user1'

// app/config/config.yaml
doctrine:
dbal:
    default_connection: default
    connections:
        #default
        default:
            driver:   pdo_mysql
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
        #user1
        user1:
            driver:   pdo_mysql
            host:     "%database_host_user1%"
            port:     "%database_port_user1%"
            dbname:   "%database_name_user1%"
            user:     "%database_user_user1%"
            password: "%database_password_user1%"
            charset:  UTF8
orm:
    default_entity_manager: default
    entity_managers:
        #default
        default:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            auto_mapping: false
            connection: default
            mappings:
                AppBundle:  ~
        #user1
        user1:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            auto_mapping: false
            connection: user1
            mappings:
                AppBundle:  ~

现在,当我为数据库创建实体时,就像这样

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Table(name="app_clients")
 * @ORM\HasLifecycleCallbacks
 * @ORM\Entity(repositoryClass="AppBundle\Entity\ClientRepository")
 */
class Client
{

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;


/**
 * @ORM\Column(type="integer")
 */
protected $mailbox;
}

使用php app/console doctrine:schema:update --force 它始终将其放入nm_admin数据库(默认值)。 Doctrine的文档没有说明选择模式here,但有没有办法?我希望某些实体属于nm_user,有些实体属于nm_admin,我该如何定义?

1 个答案:

答案 0 :(得分:3)

尝试通过这种方式更新第二个数据库:

php app/console doctrine:schema:update --force --em=user1

并查看:http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html