简体   繁体   English

PHP-Zend Framework 2中处理多个数据库的最佳方法

[英]PHP - Best Way To Handle Multiple database in Zend framework 2

We are creating a web application using Zend Framework 2 with multiple databases. 我们正在使用Zend Framework 2和多个数据库创建一个Web应用程序。 I have a core database which loads info of all customers. 我有一个core数据库,可加载所有客户的信息。 This database contains customer table. 该数据库包含customer表。 The fields of customer table are :: 客户表的字段为::

  1. id ID
  2. username 用户名
  3. password 密码
  4. database_name 数据库名称
  5. customer_name 顾客姓名
  6. ................... ...................

When a customers logs in, i have to load his database name from the core database and then make query requests to the database. 当客户登录时,我必须从core数据库加载他的数据库名称,然后向数据库提出查询请求。

I cannot have multiple adapters either, because all customers have their own database which i have to load from customer table of core_db ! 我也不能有多个适配器,因为所有客户都有自己的数据库,我必须从core_db customer表中core_db

I thought i would prefix database name with table name. 我以为我会以表名作为数据库名的前缀。

I tried this in Module.php : 我在Module.php尝试了这个:

"CategoryTableGateway" => function ($sm) {
                $dbAdapter = $sm->get("Zend\Db\Adapter\Adapter");
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Category());
                return new TableGateway("databasename.category", $dbAdapter, null, $resultSetPrototype);
            }

I had configured default database in my config\\autoload\\database.global.php as this: 我在config\\autoload\\database.global.php配置了默认数据库,如下所示:

    'db' => array(
    'driver'         => 'Pdo',
    'dsn'            => 'mysql:dbname=core_db;host=localhost',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
)

I got a exception like: 我有一个异常,如:

Base table or view not found: 1146 Table 'core_db.databasename.category' doesn't exist.

And then, I removed dbname=core_db from config\\autoload\\database.global.php . 然后,我从config\\autoload\\database.global.php删除了dbname=core_db

Now, I got another exception like: 现在,我又遇到了另一个异常:

SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

So how do i handle that situation in Zend Framework 2. I am new to Zend Framework 2. 因此,我该如何在Zend Framework 2中处理这种情况。我是Zend Framework 2的新手。

Edit : I got the solution to my question by myself. Edit :我自己解决了我的问题。

To connect to table of another schema you need to pass TableIdentifier instead of table! 要连接到另一个模式的表,您需要传递TableIdentifier而不是table!

For example, 例如,

Instead of: 代替:

 $CategoryTableGateway = new TableGateway("category", $dbAdapter, null, $resultSetPrototype);

You have to do: 你必须做:

$CategoryTableIdentifier = new TableIdentifier('category','dbname');
$CategoryTableGateway = new TableGateway($CategoryTableIdentifier, $dbAdapter, null, $resultSetPrototype);

Hope It Works! 希望它有用!

When a customers logs in, i have to load his database name from the core database and then make query requests to the database. 当客户登录时,我必须从核心数据库加载他的数据库名称,然后向数据库提出查询请求。

This is what you are doing wrong. 这就是你做错了。

Just keep everything in single database, like very e-commerce site in the world does. 只需将所有内容保存在单个数据库中,就像世界上非常电子商务的网站一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM