一个实体管理器和多个数据库不能检测模式的变化 [英] Doctrine not detecting schema changes with one entity manager and multiple databases
问题描述
并设置多个连接和一个实体管理器。这里是 app / config / config.yml
:
doctrine:
dbal:
default_connection:db1
连接:
db1:
驱动程序:pdo_mysql
主机:127.0.0.1
dbname:db1
db2:
驱动程序:pdo_mysql
主机:127.0.0.1
dbname:db2
orm:
default_entity_manager:默认
auto_generate_proxy_classes:%kernel.debug%
entity_managers:
默认值:
auto_mapping:true
映射:
FirstBundle:
类型:注释
dir:模型
前缀:NoiseLabs\FirstBundle\Model
SecondBundle:
type:annotation
dir:模型
前缀:NoiseLabs\SecondBundle\Model
实体类在code> FirstBundle :
命名空间NoiseLabs\FirstBundle\Model;
/ **
* @ ORM\Entity
* @ ORM\Table(name =db1.table1)
* /
class FirstEntity
{
/ **
* @ ORM\Id
* @ ORM\Column(name =id,type =integer)
* @ ORM\GeneratedValue(strategy =AUTO)
* /
protected $ id;
}
实体类在 SecondBundle
:
命名空间NoiseLabs\SecondBundle\Model;
/ **
* @ ORM\Entity
* @ ORM\Table(name =db2.table2)
* /
class SecondEntity
{
/ **
* @ ORM\Id
* @ ORM\Column(name =id,type =integer)
* @ ORM\GeneratedValue(strategy =AUTO)
* /
protected $ id;
/ **
* @ ORM\ManyToOne(targetEntity =NoiseLabs\FirstBundle\Model\FirstEntity)
* @ ORM\JoinColumn(name = firstId,referencedColumnName =id,onDelete =CASCADE)
* /
protected $ first;
}
现在,问题是控制台原则:schema:update --dump-sql
只检测(默认连接) db1.tables
中的模式更改。如果我将默认连接设置为 db2
,它将只输出与 db2.tables
相关的sql。
我已经使用 app / console doctrine:mapping:info
和所有实体类进行了映射。
这是Doctrine / Symfony的限制,还是需要调整我的配置?谢谢。
每个实体管理器只能有一个连接。将默认实体管理器分为两个。 app / console doctrine:schema:update
采用可选参数( em
)来指定正在使用的实体管理器。你必须运行两次:
app / console doctrine:schema:update --dump-sql em =default
app / console doctrine:schema:update --dump-sql em =my_second_em
还有一篇很短的文章(如何使用多个实体管理器和连接)在Symfony2食谱值得一读。
Using Symfony-2.1 and Doctrine-2.3. I have multiple databases and need to do cross-database joins so I followed the suggestions in:
- Multiple database connection in Doctrine2 and Zend framework
- Using Relationships with Multiple Entity Managers
and set up multiple connections and one entity manager. Here is app/config/config.yml
:
doctrine:
dbal:
default_connection: db1
connections:
db1:
driver: pdo_mysql
host: 127.0.0.1
dbname: db1
db2:
driver: pdo_mysql
host: 127.0.0.1
dbname: db2
orm:
default_entity_manager: default
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
auto_mapping: true
mappings:
FirstBundle:
type: annotation
dir: Model
prefix: NoiseLabs\FirstBundle\Model
SecondBundle:
type: annotation
dir: Model
prefix: NoiseLabs\SecondBundle\Model
Entity class in FirstBundle
:
namespace NoiseLabs\FirstBundle\Model;
/**
* @ORM\Entity
* @ORM\Table(name="db1.table1")
*/
class FirstEntity
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
Entity class in SecondBundle
:
namespace NoiseLabs\SecondBundle\Model;
/**
* @ORM\Entity
* @ORM\Table(name="db2.table2")
*/
class SecondEntity
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="NoiseLabs\FirstBundle\Model\FirstEntity")
* @ORM\JoinColumn(name="firstId", referencedColumnName="id", onDelete="CASCADE")
*/
protected $first;
}
Now, the problem is app/console doctrine:schema:update --dump-sql
only detects schema changes in (default connection) db1.tables
. If I set the default connection to db2
it will only output sql related to db2.tables
.
I've checked with app/console doctrine:mapping:info
and all entity classes are being mapped.
Is this a limitation of Doctrine/Symfony or do I need to adjust my configuration? Thanks.
Each entity manager can have only one connection. Split the default entity manager into two. app/console doctrine:schema:update
takes an optional argument (em
) to specify the entity manager in use. You'd have to run it twice:
app/console doctrine:schema:update --dump-sql em="default"
app/console doctrine:schema:update --dump-sql em="my_second_em"
There's also a short article (How to work with Multiple Entity Managers and Connections) in the Symfony2 cookbook that's worth reading.
这篇关于一个实体管理器和多个数据库不能检测模式的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!