php-sql-query-builder icon indicating copy to clipboard operation
php-sql-query-builder copied to clipboard

Database + table name

Open andybee opened this issue 7 years ago • 4 comments

I was expecting to be able to build a query along the lines of:

$queryBuilder->select()->setTable('users.user');

Where the table user exists within a database users.

Am I missing an undocumented method that lets me set the DB another way, or do I have to apply a USE SQL command ahead of running the query?

andybee avatar Feb 02 '18 18:02 andybee

The library appears to support this through the Table::__construct(.., $schema) parameter and Table::setSchema(), which are also consumed correctly by the Writer implementation.

The only problem is, it seems that the query builder class does not expose this functionality. The $builder->select()->setTable() method only allows a string, or an array with name/alias. It does not allow a schema, nor does it allow a custom Table object to be passed in.

@nilportugues Do I understand correctly? If so, what approach would you prefer for solving this? I would be interested in writing a patch for to fix this.

Krinkle avatar Nov 30 '18 01:11 Krinkle

@andybee we use query builder in association with PDO, hence we sconnect and select the database through PDO, before using query builder. You can try that as well, hope this answer is not too late?

marcus-hiles avatar Mar 29 '19 11:03 marcus-hiles

@marcus-hiles The issue I was experiencing is that in our multi-tenant environment (where we use databases to separate concerns), the same script may need to use multiple different databases and re-establishing the connection to the server just to switch DB would be inefficient.

andybee avatar Apr 05 '19 10:04 andybee

Oh ok, gotcha @andybee Then I think your best bet here is to mysqli. Back to the basics man :-)

marcus-hiles avatar Apr 09 '19 19:04 marcus-hiles