php - Is it possible to use two diffrent document managers with different databases for the same bundle in Symfony? -


my goal have 2 different document managers connected different databases share same database models.

i have heavily changed database model , write custom migration script retrieves objects old model reads values , creates new object on new schema information of old object.

i have found related stackoverflow question here: working 2 entity managers in same bundle in symfony2

howewer, solution suggests use different prefixes each database , stores classes data model in different folders:

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:                   # must specify type                   type:     "annotation"                       # directory entity (relative bundle path)                   dir:      "entity/firstdb"                           #the prefix                    prefix:   "your\bundle\entity\firstdb"          shop:             connection:       second             mappings:                 yourbundle:                   type: "annotation"                   #here second path entity connection stand                   dir: "entity/seconddb"                    #the prefix                   prefix: "your\bundle\entity\seconddb"  

i have 2 different document manager objects share same models in same folder connected diffrent databases. possible?

i found out indeed possible connected different databases in same symfony bundle. answer led me possible solution: 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 db name prefix use when selecting database connect      *      * @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();              // check if current connection needs closed based on various things             // have left part in information here             // $appid changed in 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 parameters parent             $connection->__construct(                 $params, $connection->getdriver(), $connection->getconfiguration(),                 $connection->geteventmanager()             );              try {                 $connection->connect();             } catch (exception $e) {                 // log , handle exception             }         }          return $this;     } } 

with solution above 1 can write service can called change database current entity manager connected to different database.

unfortunately solution above works pdo driver (mysql). since our technology stack includes mongodb , use doctrine-mongodb bundle had differnt solution.

the doctrine-mongodb documentation has section setting custom document manager here: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html

i unable right doctrine-mongodb odm document mappings work instructions in doc , therefore solution me create simple php mongoclient connection:

$mongo = new \mongo('mongodb://localhost:27017'); $legacydbdocumentmangager = $mongo->selectdb('backed_up_prod_db'); $legacyusercollection = $legacydbdocumentmangager->selectcollection('user'); $user = $legacyusercollection->findone(array('email' => 'matyas@stackoverflow.com')); 

the differnce between simple php mongodb driver , doctrine-mongodb odm results of queries of driver associative arrays , results of doctrine-mongodb odm diver objects.

i hope information useful someone.


Comments