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

发布于 2024-12-12 03:12:02 字数 1586 浏览 0 评论 0原文

我有某种形式,并且一列具有 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();
    }

}

但没有成功:(

I have some form, and one column have Entity type, but this entity have another connection.

In action i can $em->getDoctrine()->getEntityManager('name')

How to change connection in form class?

Maybe can possibly change connection in entity class.
Like this

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

UPDATED:

I found answer here :)

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

But how i can change connection in data fixtures class?

I try:

<?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();
    }

}

but didnt work :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-19 03:12:02

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

执行装置

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

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

在装置中使用容器

如果您的固定装置类可以访问容器,那么您可以加载您想要的任何实体管理器。

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

When loading fixtures you can use the flag when executing the console command to change the entity manager:

Executing Fixtures

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

Alternatively you could check out this section in the same docs:

Using the container in fixtures

If your fixture class has access to the container then you could load any entity manager you wish.

$container->get('doctrine')->getEntityManager('manager_name');
风吹短裙飘 2024-12-19 03:12:02

如果您的装置可以访问容器,并且您的实际配置位于 config.yml 中:
<代码>
奥姆:
default_entity_manager:默认在此处输入代码
实体经理:
所有者:
联系方式: 业主
映射:
房地产核心捆绑包:
实体:我的实体

你可以这样调用实体管理器:

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

If your fixture has access to container, with your actual config in config.yml :

orm:
default_entity_manager: default
enter code here
entity_managers:
owner:
connection: owner
mappings:
RealestateCoreBundle:
Entity: MyEntity

you can call the entity manager like that :

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文