How to query with multiple databases connection with propel

520 Views Asked by At

I have made a lot of researches to read several documentation but I didn't find an answer.

I have 2 databases connected through my propel config. They have the same structure, the same tables.

At this moment, I created a schema.xml for each database which are: db1.schema.xml and db2.schema.xml

I modified my config.php:

<?php
$serviceContainer = \Propel\Runtime\Propel::getServiceContainer();
$serviceContainer->checkVersion('2.0.0-dev');
$serviceContainer->setAdapterClass('db1', 'mysql');
$manager = new \Propel\Runtime\Connection\ConnectionManagerSingle();
$manager->setConfiguration(array (
  'dsn' => 'mysql:host=localhost;port=3306;dbname=db1',
  'user' => 'root',
  'password' => '',
  'settings' =>
  array (
    'charset' => 'utf8',
    'queries' =>
    array (
    ),
  ),
  'classname' => '\\Propel\\Runtime\\Connection\\ConnectionWrapper',
  'model_paths' =>
  array (
    0 => 'src',
    1 => 'vendor',
  ),
));
$manager->setName('db1');
$serviceContainer->setConnectionManager('db1', $manager);
$serviceContainer->setDefaultDatasource('db1');

$serviceContainer2 = \Propel\Runtime\Propel::getServiceContainer();
$serviceContainer2->checkVersion('2.0.0-dev');
$serviceContainer2->setAdapterClass('db2', 'mysql');
$manager2 = new \Propel\Runtime\Connection\ConnectionManagerSingle();
$manager2->setConfiguration(array (
  'dsn' => 'mysql:host=localhost;port=3306;dbname=db2',
  'user' => 'root',
  'password' => '',
  'settings' =>
  array (
    'charset' => 'utf8',
    'queries' =>
    array (
    ),
  ),
  'classname' => '\\Propel\\Runtime\\Connection\\ConnectionWrapper',
  'model_paths' =>
  array (
    0 => 'src',
    1 => 'vendor',
  ),
));
$manager2->setName('db2');
$serviceContainer2->setConnectionManager('db2', $manager);
$serviceContainer2->setDefaultDatasource('db2');

I also modified my propel.json file:

{
    "propel": {
        "database": {
            "connections": {
                "db1": {
                    "adapter": "mysql",
                    "dsn": "mysql:host=localhost;port=3306;dbname=db1",
                    "user": "root",
                    "password": null,
                    "settings": {
                        "charset": "utf8"
                    }
                },
                "db2": {
                    "adapter": "mysql",
                    "dsn": "mysql:host=localhost;port=3306;dbname=db2",
                    "user": "root",
                    "password": null,
                    "settings": {
                        "charset": "utf8"
                    }
                }
            }
        },
        "runtime": {
            "defaultConnection": "db1",
            "connections": ["db1", "db2"]
        },
        "generator": {
            "defaultConnection": "db1",
            "connections": ["db1","db2"]
        }
    }
}

It's now from here, I don't know what to do. How can I specify in my request, which database I want to use?


<?php
require_once 'vendor/autoload.php';

require_once 'generated-conf/config.php';

use nhlonline\AuthorQuery;

$author = AuthorQuery::create()->find();

foreach($author as $pub){
   echo $pub->getFirstName() . '<br>';
}


?>

And even if I try this, I have an error (My AuthorQuery class file is correctly in the folder):

Fatal error: Uncaught Error: Class 'Base\AuthorQuery' not found in C:\xampp\htdocs\nhlonline\nhlonline\AuthorQuery.php:15 Stack trace: #0 C:\xampp\htdocs\nhlonline\vendor\composer\ClassLoader.php(444): include() #1 C:\xampp\htdocs\nhlonline\vendor\composer\ClassLoader.php(322): Composer\Autoload\includeFile('C:\xampp\htdocs...') #2 [internal function]: Composer\Autoload\ClassLoader->loadClass('nhlonline\Autho...') #3 C:\xampp\htdocs\nhlonline\test.php(8): spl_autoload_call('nhlonline\Autho...') #4 {main} thrown in C:\xampp\htdocs\nhlonline\nhlonline\AuthorQuery.php on line 15

1

There are 1 best solutions below

0
Jacob Thomason On

You need to pass the respective connection object to the find() method. Check the interface:

find(ConnectionInterface $con = null)