更改表单类和 Dotrine Fixtures 上的 Doctrine 连接
我有某种形式,并且一列具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
加载固定装置时,您可以在执行控制台命令时使用该标志来更改实体管理器:
执行装置
或者,您可以在同一文档中查看此部分:
在装置中使用容器
如果您的固定装置类可以访问容器,那么您可以加载您想要的任何实体管理器。
When loading fixtures you can use the flag when executing the console command to change the entity manager:
Executing Fixtures
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.
如果您的装置可以访问容器,并且您的实际配置位于 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 :
enter code hereorm:
default_entity_manager: default
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');