Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

We are creating a web application using Zend Framework 2 with multiple databases. I have a core database which loads info of all customers. This database contains customer table. The fields of customer table are ::

  1. 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.

I cannot have multiple adapters either, because all customers have their own database which i have to load from customer table of core_db!

I thought i would prefix database name with table name.

I tried this in 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:

    '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.

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.

Edit: I got the solution to my question by myself.

To connect to table of another schema you need to pass TableIdentifier instead of 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!

share|improve this question

1 Answer 1

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.

share|improve this answer
    
Thanks for your advice. Can you give some examples? That would be appreciated. –  Ujjwal Ojha Oct 26 '13 at 10:51
    
Examples of what? –  Your Common Sense Oct 26 '13 at 10:58
    
Forget the E-commerce part. I have changed the question. It was my mistake. My question is more about handing multiple databases! –  Ujjwal Ojha Oct 26 '13 at 11:01
    
it doesn't matter. You don't understand the meaning of database and inventing a square wheel. Just use one. That's all. –  Your Common Sense Oct 26 '13 at 11:04
    
That means I have to user where "customer_id=$id" in all query. –  Ujjwal Ojha Oct 26 '13 at 11:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.