Doctrine not detecting schema changes with one ent

2019-03-31 22:57发布

问题:

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.

回答1:

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.