多实体管理器,选择错误的数据库

时间:2013-09-18 19:57:39

标签: php symfony doctrine-orm


您好, 我在Symfony 2中使用多个实体管理器,但在表单验证期间,handleRequest方法使用了错误的实体管理器。

这是我的ORM配置:

dbal:
        default_connection: customer_database
        connections:
            customer_database:
                driver:   %database_driver%
                host:     %database_host%
                port:     %database_port%
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                charset:  UTF8
            shared_database:
                driver:   %shared_database_driver%
                host:     %shared_database_host%
                port:     %shared_database_port%
                dbname:   %shared_database_name%
                user:     %shared_database_user%
                password: %shared_database_password%     
orm:
        auto_generate_proxy_classes: %kernel.debug%
        default_entity_manager: customer_em
        entity_managers:
            shared_em:
                connection: shared_database
                mappings:
                    KnFEngineBundle:
                        type: "annotation"
                        dir: "Entity/Shared"
            customer_em:
                connection: customer_database
                mappings:
                    KnFEngineBundle:
                        type: "annotation"
                        dir: "Entity/Customer"
                    KnFModTextBundle: ~
                    KnFModMapBundle: ~
                    KnFModFormBundle: ~

我的控制器中验证表单的函数:

public function addAction( $isLink )
{
    $kernel = $this->container->get( 'knf_engine.kernel' );
    $form = $this->createForm( new PageType( $isLink, $kernel->getAvailablePageOwner() ) );

    $request = $this->get( 'request' );
    if( $request->getMethod() == 'POST' )
    {
        $page = new Page( $isLink );
        $form->setData( $page );
        $form->handleRequest( $request ); //the function call that throw error
        if( $form->isValid() )
        {   
            $kernel->addPage( $page );
            return $this->redirect( $this->generateUrl( 'knf_engine_admin_page' ) );
        }
    }

    $args = array(  'form' => $form->createView(),
                    'url' => $this->getURL(),
                    'isLink' => $isLink );

    if( $request->isXmlHttpRequest() )
    {
        return $this->render( 'KnFEngineBundle:admin:includes/pageadd_modal.html.twig', $args );
    }
    else
    {           
        return $this->render(   'KnFEngineBundle:admin:pageadd.html.twig', $args );
    }
}

错误:

An exception occurred while executing 'SELECT t0.id AS id1, t0.isLink AS isLink2, t0.externalLink AS externalLink3, t0.isPopup AS isPopup4, t0.address AS address5, t0.position AS position6, t0.title AS title7, t0.active AS active8, t0.owner_id AS owner_id9 FROM Page t0 WHERE t0.address = ?' with params ["dhfg"]:
    SQLSTATE[42S02]: Base table or view not found: 1146 Table 'kreanet_framework_global.page' doesn't exist 

但实体是“Page”在Entity / Customer文件夹中定义,该文件映射到客户实体管理器(默认em)。如您所见,doctrine尝试访问其他实体管理器中的数据。

经过调查,我理解了doctrine将表单与handleRequest()方法中的数据库绑定(我之前使用过bind()但现在已弃用)。 我真的不知道为什么教条会在那个数据库中查找。

感谢阅读并抱歉我的英语,这不是我的母语,但我希望你能理解我的问题。

1 个答案:

答案 0 :(得分:0)

我有类似的问题。但就我而言,我正在使用

@UniqueEntity(fields={"field1", "field2"})

所以问题不是entityManager,而是UniqueEntity限制了解决方案:

@UniqueEntity(fields={"field1", "field2"}, em="entityManagerName")