2016-08-08 5 views
1

저의 목표는 동일한 데이터베이스 모델을 공유하는 서로 다른 데이터베이스에 연결된 두 개의 서로 다른 문서 관리자를 만드는 것입니다.Symfony에서 동일한 번들에 대해 서로 다른 데이터베이스를 가진 두 개의 다른 문서 관리자를 사용할 수 있습니까?

데이터베이스 모델을 많이 변경했으며 이전 모델의 개체를 검색하는 사용자 지정 마이그레이션 스크립트를 작성하여 해당 값을 읽은 다음 이전 개체 정보를 사용하여 새 스키마에 새 개체를 만듭니다.

내가 여기에 관련 유래 질문 발견

: Working with two entity managers in the same bundle in Symfony2

Howewer,이 솔루션은 각 데이터베이스에 대해 다른 접두사를 사용하는 제안과는 다른 폴더에있는 데이터 모델을위한 클래스를 저장합니다

doctrine: 
dbal: 
    default_connection: default 
    connections: 
     default: 
      driver: %database_driver% 
      host:  %database_host% 
      port:  %database_port% 
      dbname: %database_name% 
      user:  %database_user% 
      password: %database_password% 
      charset: UTF8 
     second: 
      driver: %database_sqlite_driver% 
      host:  ~ 
      port:  ~ 
      dbname: %database_sqlite_shop_name% 
      path:  %database_sqlite_shop_name% 
      user:  ~ 
      password: ~ 
      charset: UTF8 

orm: 
    auto_generate_proxy_classes: %kernel.debug% 
    default_entity_manager: default 
    entity_managers: 
     default: 
      connection:  default 
      mappings: 
       YourBundle: 
        # you must specify the type 
        type:  "annotation"  
        # The directory for entity (relative to bundle path) 
        dir:  "Entity/FirstDb"   
        #the prefix 
        prefix: "Your\Bundle\Entity\FirstDb" 
     shop: 
      connection:  second 
      mappings: 
       YourBundle: 
        type: "annotation" 
        #here the second path where entity for the connection stand 
        dir: "Entity/SecondDb" 
        #the prefix 
        prefix: "Your\Bundle\Entity\SecondDb" 

같은 폴더에서 동일한 모델을 공유하지만 다른 데이터베이스에 연결된 두 개의 다른 문서 관리자 개체를 갖고 싶습니다. 이것이 가능한가?

+1

동일한 문서를 두 DocumentManager 모두에 매핑하려고하면 오류가 발생합니까? – malarzm

+0

예 문서 관리자가 더 이상 문서를 검색하지 않습니다. 내가 쿼리하는 모든 문서에 대해 아무 것도 얻지 못합니다. 나는 이것이이 구성이 만들어내는 충돌과 관련이 있다고 가정한다. – matyas

+1

수정하십시오. mongo : [심포니에서 여러 mongodb 문서 관리자와 연결 사용하기] (http://www.inanzzz.com/index.php/post/fukm/using-multiple-mongodb-document-managers-and-connections-in-symfony)) 및 For MySQL : [여러 번들 및 데이터베이스에 다중 MySQL 엔터티 관리자 사용하기] (http://www.inanzzz.com/index.php/post/7cpx/using-multiple-mysql-entity-managers-for-multiple- 번들 및 데이터베이스) – BentCoder

답변

0

같은 Symfony 번들의 다른 데이터베이스에 실제로 연결할 수 있다는 것을 알았습니다. 이 답변은 가능한 해결책를 알려준 : https://stackoverflow.com/a/15110867/2174832

#services.yml 
acme_app.dynamic_connection: 
class: %acme.dynamic_doctrine_connection.class% 
calls: 
    - [setDoctrineConnection, @doctrine.dbal.default_connection]] 


<?php 

namespace Acme\Bundle\AppBundle; 

use Doctrine\DBAL\Connection; 
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; 
use Exception; 

class DynamicDoctrineConnection 
{ 
    /** 
    * @var Connection 
    */ 
    private $connection; 

    /** 
    * Sets the DB Name prefix to use when selecting the database to connect to 
    * 
    * @param Connection  $connection 
    * @return SiteDbConnection $this 
    */ 
    public function setDoctrineConnection(Connection $connection) 
    { 
     $this->connection = $connection; 

     return $this; 
    } 

    public function setUpAppConnection() 
    { 
     if ($this->request->attributes->has('appId')) { 
      $connection = $this->connection; 
      $params  = $this->connection->getParams(); 

      // we also check if the current connection needs to be closed based on various things 
      // have left that part in for information here 
      // $appId changed from that in the connection? 
      // if ($connection->isConnected()) { 
      //  $connection->close(); 
      // } 

      // Set default DB connection using appId 
      //$params['host'] = $someHost; 
      $params['dbname'] = 'Acme_App'.$this->request->attributes->get('appId'); 

      // Set up the parameters for the parent 
      $connection->__construct(
       $params, $connection->getDriver(), $connection->getConfiguration(), 
       $connection->getEventManager() 
      ); 

      try { 
       $connection->connect(); 
      } catch (Exception $e) { 
       // log and handle exception 
      } 
     } 

     return $this; 
    } 
} 

을 솔루션을 하나는 현재의 엔티티 매니저가 다른 데이터베이스에 연결되어있는 데이터베이스를 변경하기 위해 호출 할 수있는 서비스를 작성할 수 위.

위의 해결책은 위의 솔루션은 PDO 드라이버 (Mysql)에서만 작동합니다. 우리의 기술 스택에는 mongodb이 포함되어 있으므로 교리 - 몽고 닷 번들을 사용하십시오. differnt solution을 찾아야했습니다.

교리 - MongoDB의 문서는 여기에 사용자 지정 문서 관리자 설정에 대한 섹션이 있습니다 http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html

나는 올바른 교리 MongoDB의 ODM 문서 매핑이 지시 사항 문서에 따라서 작업을 얻을 수 없습니다 나에게 유일한 해결책은 간단한 PHP mongoclient 연결을 만드는 것이었다 :

$mongo = new \Mongo('mongodb://localhost:27017'); 
$legacyDbDocumentMangager = $mongo->selectDB('backed_up_prod_db'); 
$legacyUserCollection = $legacyDbDocumentMangager->selectCollection('User'); 
$user = $legacyUserCollection->findOne(array('email' => '[email protected]')); 

이 간단한 PHP MongoDB의 드라이버와 교리 MongoDB를의 ODM의 유일한 differnce이 드라이버의 쿼리의 결과가 연관 배열이 있다는 것입니다 및 D의 결과 octrine-mongodb odm 다이버는 대상입니다.

이 정보가 도움이되기를 바랍니다.

관련 문제