更改表单类和Doctrine Fixtures上的doctrine连接

时间:2011-10-27 13:27:35

标签: forms doctrine-orm symfony

我有一些表单,一列有Entity类型,但此实体有另一个连接。

在行动中我可以$em->getDoctrine()->getEntityManager('name')

如何更改表单类中的连接?

也许可以改变实体类中的连接。 喜欢这个

orm:
    default_entity_manager: default
    entity_managers:
        owner:
            connection: owner
            mappings:
                RealestateCoreBundle:
                    Entity: MyEntity

更新:

我在这里找到答案:)

http://symfony.com/doc/2.0/reference/forms/types/entity.html#em

但是我如何才能改变数据夹具类的连接?

我试试:

<?php

namespace Realestate\CoreBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Realestate\CoreBundle\Entity\Owner;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class OwnerFixtures implements FixtureInterface, ContainerAwareInterface
{

    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function load($manager)
    {
        $this->container->get('doctrine')->getEntityManager('owner');

        for ($i = 0; $i < 100; $i++) {
            $owner = new Owner();
            $owner->setName('name-' . $i);
            $owner->setTelephone(mt_rand(100000, 999999));
            $manager->persist($owner);
        }

        $manager->flush();
    }

}

但没有工作:(

2 个答案:

答案 0 :(得分:0)

加载灯具时,您可以在执行控制台命令时使用该标志来更改实体管理器:

Executing Fixtures

php app/console doctrine:fixtures:load --em=manager_name

或者您可以在相同的文档中查看此部分:

Using the container in fixtures

如果您的fixture类可以访问容器,那么您可以加载任何实体管理器。

$container->get('doctrine')->getEntityManager('manager_name');

答案 1 :(得分:0)

如果您的灯具可以访问容器,请使用config.yml中的实际配置: orm: default_entity_manager: default在此输入代码 entity_managers: owner: connection: owner mappings: RealestateCoreBundle: Entity: MyEntity

你可以像这样调用实体经理:

$manager = $this->container->get('doctrine.orm.owner_entity_manager');