我做了很多研究以阅读一些文档,但没有找到答案。

我通过propel config连接了2个数据库。它们具有相同的结构,相同的表。

此时,我为每个数据库创建了一个schema.xml:
db1.schema.xml和db2.schema.xml

我修改了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');


我还修改了propel.json文件:

{
    "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"]
        }
    }
}


现在是从这里开始,我不知道该怎么办。如何在请求中指定要使用的数据库?


<?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>';
}


?>


即使我尝试这样做,也会出现错误(我的AuthorQuery类文件正确位于文件夹中):


  致命错误:未捕获的错误:找不到类“ Base \ AuthorQuery”
  C:\ xampp \ htdocs \ nhlonline \ nhlonline \ AuthorQuery.php:15堆栈跟踪:#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 [内部
  功能]:
  Composer \ Autoload \ ClassLoader-> loadClass('nhlonline \ Autho ...')#3
  C:\ xampp \ htdocs \ nhlonline \ test.php(8):
  spl_autoload_call('nhlonline \ Autho ...')#4 {main}被抛出
  第15行的C:\ xampp \ htdocs \ nhlonline \ nhlonline \ AuthorQuery.php

最佳答案

您需要将各个连接对象传递给find()方法。检查界面:

find(ConnectionInterface $con = null)

关于php - 如何使用propel查询多个数据库连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58296953/

10-12 16:44