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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵