2012-12-26 34 views
3

使用Symfony-2.1和Doctrine-2.3。我有多個數據庫和需要做跨數據庫連接,所以我跟着建議中:不使用一個實體管理器和多個數據庫檢測模式更改的原則

,並建立多個連接和一個實體管理器。這裏是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 

實體類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; 
} 

實體類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; 
} 

現在,問題是app/console doctrine:schema:update --dump-sql僅檢測(默認連接模式更改)db1.tables。如果我將默認連接設置爲db2,它只會輸出與db2.tables相關的sql。

我檢查了app/console doctrine:mapping:info並且所有實體類都被映射。

這是Doctrine/Symfony的限制還是我需要調整我的配置?謝謝。

回答

0

每個實體管理器只能有一個連接。將默認的實體管理器分成兩部分。 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的食譜是值得閱讀的短文(How to work with Multiple Entity Managers and Connections)。

+0

在一個webapp中,我維護着10個Bundle和7個數據庫。對於每個EntityManager(又名數據庫)的這種配置,我需要爲所有這10個Bundle設置/重複相同的映射,否則在鏈配置的名稱空間*中找不到「* Class X」。沒有更好的方法來做到這一點? – noisebleed

+0

@noisebleed:我以前爲兩個數據庫中的實體運行了兩個查詢,並使用array_map或類似的代碼將它們結合在控制器代碼中。映射問題令人頭疼。我最終將它們移至同一個數據庫中完美工作的兩個模式。 – Lighthart

相關問題