2013-10-22 62 views
0

我是新的Symfony2用戶,我需要幫助! 我有兩個捆綁包與實體:Symfony 2中不同連接之間的關係

// My\FooBundle\Entity\Foo.php 
namespace My\FooBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Table(name="foo") 
* @ORM\Entity 
*/ 
class Foo 
{ 

    /*...*/ 

    /**   
    * @ORM\OneToOne(targetEntity="My\BarBundle\Entity\Bar") 
    */ 
    private $bar; 
} 

而另一束:

// My\BarBundle\Entity\Bar.php 
namespace My\BarBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Table(name="bar") 
* @ORM\Entity 
*/ 
class Bar 
{ 
    /*...*/ 

    /** 
    * @ORM\Column(name="name", nullable=false) 
    */ 
    private $name; 
} 

而且我config.yml

doctrine: 
    dbal: 
     default_connection: foo 
     connections: 
      foo:     
       dbname: "foo"     
      bar:    
       dbname: "bar" 
    orm:    
     entity_managers: 
      foo: 
       connection:  foo 
       mappings: 
        MyFooBundle: ~ 
        MyBarBundle: ~ 
      bar: 
       connection:  bar 
       mappings: 
        MyBarBundle: ~ 

和SF在美孚數據庫中創建欄。 問:如何在這種情況下創建兩個連接之間的關係?

回答

1

刪除MyBarBundle捆綁從foo連接。

+0

它告訴我一個錯誤,當我嘗試執行'學說:架構:update --dump-sql': '我的班'My \ BarBundle \ Entity \ Bar'沒有在t 找到他鏈配置的命名空間My \ FooBundle \ Foo' – Vladowski

+0

你有'My \ FooBundle \ Entity中使用My \ BarBundle \ Entity \ Bar' – goto

+0

http://stackoverflow.com/questions/11463517/using-relationships-with-multiple-entity-managers – Udan

0

添加數據庫名稱酒吧實體

// My\BarBundle\Entity\Bar.php 
namespace My\BarBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Table(name="bar.bar") 
* @ORM\Entity 
*/ 
class Bar 
{ 
    /*...*/ 

    /** 
    * @ORM\Column(name="name", nullable=false) 
    */ 
    private $name; 
} 

與以下字符串config.yml

doctrine: 
    dbal: 
     default_connection: foo 
     connections: 
      foo:     
       dbname: "foo"     
      bar:    
       dbname: "bar" 
    orm:    
     entity_managers: 
      foo: 
       connection:  foo 
       mappings: 
        MyFooBundle: ~ 
        MyBarBundle: 
         type:  "annotation"  
         dir:  ..\..\My\BarBundle\Entity  
      bar: 
       connection:  bar 
       mappings: 
        MyBarBundle: 
         type:  "annotation"  
         dir:  ..\..\My\BarBundle\Entity