Symfony 2中不同连接之间的关系

时间:2013-10-22 12:46:19

标签: php symfony doctrine-orm

我是新的Symfony2用户,我需要帮助! 我有两个带实体的Bundles:

// 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在Foo数据库中创建了Bar。 问:在这种情况下,如何在两个连接之间创建关系?

2 个答案:

答案 0 :(得分:1)

MyBarBundle连接中删除foo个捆绑包。

答案 1 :(得分: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