doudouwen2763 2015-04-14 18:43
浏览 62
已采纳

如何在Symfony2表单上动态选择EntityManager以与ManyToMany一起使用

I have a Symfony2 bundle where I am utilizing a dynamic selection of EntityManagers based on the subdomain in addition to a fixed EM for general settings, etc. For example, a user lands on dev.mydomain.com and is presented with a login screen that pulls information from a default database containing site title, colors, etc. The login script, however, references the dev database which contains the users and data for that subdomain. Similarly, when logging into other.mydomain.com, the login references the other database. This all works great and users are validated against their appropriate databases.

The issue I'm encountering is when I create a "new user form" using Symfony's Form system. I utilize a Many to Many relationship for user roles as outlined by The Book, but can't find a way to specify which EntityManager is used, causing it to look for the relationships against the default EntityManager.

Controller/UserController.php

public function addAction(Request $request) {

    $us = new User();

    // ORM Connection name stored in session from the login screen
    $em = $this->getDoctrine()->getManager(
        $request->getSession()->get('database')
    );

    $form = $this->createForm(
        new UserType($em), $us
    );

    return $this->render(
        'MyBundle:User:create.html.twig',
        array( 'form' => $form->createView() )
    );
}

Entity/User.php

/**
 * @var string
 *
 * @ORM\ManyToMany(targetEntity="Role", inversedBy="users", cascade={"persist"})
 */
private $roles;

config.yml

doctrine:
  dbal:
    connections:
        default:
            driver:   "%database_driver%"
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
        dev:
            driver:   "%dev.database_driver%"
            host:     "%dev.database_host%"
            port:     "%dev.database_port%"
            dbname:   "%dev.database_name%"
            user:     "%dev.database_user%"
            password: "%dev.database_password%"
            charset:  UTF8
orm:
  default_entity_manager: default
  auto_generate_proxy_classes: "%kernel.debug%"
  entity_managers:
    default:
        connection: default
        mappings:
            MyBundle: ~
    dev:
        connection: dev
        mappings:
            MyBundle: ~

Is there a way to pass a specified EntityManager into createForm or FormBuilder to be utilized by the built-in ManyToMany ORM annotation? To be clear, the rest of the form works appropriately, adding the user to the desired EntityManager -- it's just the Roles field that's still referencing default.

  • 写回答

1条回答 默认 最新

  • duancenxiao0482 2015-04-15 14:15
    关注

    The FormBuilder utilized inside of the Form/UserType.php file has add() methods, allowing one to add the components of the entity. The add() method can take an array of options as the third element. One of these options is em:

    ->add('roles', 'entity', array(
             'class'=>'MyBundle:Role',
             'property'=>'name',
             'em'=>$options['em'],
             'multiple'=>true
          ))
    

    I was able to pass the dynamically determined entity manager into the file through the createForm() method inside of Controller/User.php to be accessed from the above options array:

    $form = $this->createForm(
               new UserType($em), $us, array('em'=>$em)
            );
    

    Lastly, I ensured that the entity manager was required by including it in setDefaultOptions() in Form/UserType.php:

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setRequired(array('em'));
    
        $resolver->setDefaults(array(
            'em' => null
        ));
    }
    

    In doing this, the ManyToMany Roles field inside my form pulls from and persists to the correct entity manager.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据