Issues

ZF2-202: PHP Fatal Error in Zend\Db\Adapter\Driver\Pdo\Connection::getDefaultSchema() when using a Postgresql database

Description

There is a fatal error in Zend\Db\Adapter\Driver\Pdo\Connection::getDefaultSchema() when using a Postgresql database:

PHP Fatal error: Call to a member function fetch_row() on a non-object in /vendor/ZendFramework/library/Zend/Db/Adapter/Driver/Pdo/Connection.php on line 114

It happens because getDefaultSchema() executes "SELECT DATABASE()" to get the default schema and this function does not exist in Postgresql.

You can fix it by checking that the query was successful before attempting the fetch_row(). I will submit a pull request to that effect.

However, it is not clear that this code belongs here at all. Is the "default schema" concept generic enough to be here. I know that Postgresql has a schema search path, rather than a single schema. Consequently the default schema that new objects are created in will be the first schema in the search path that exists (there is normally a $user entry in the search path that is ignored if there is no schema matching the database username). However if you address an existing object without schema qualification then you will get the first object with the correct name that can be found along the search path.

We could attempt to be more complicated by checking for the first real schema in the search path, but there are performance implications to this and getDefaultSchema may be called a lot (I'm only guessing). In any case, my preference would be for getDefaultSchema() to return false (which is what my pull request does), or possibly null, on the basis that Postgresql doesn't really have a default schema.

Note that this solution doesnt address the question of whether database-specific SQL ("SELECT DATABASE()")should appear in the generic PDO driver.

Comments

Fixed and merged into pull request 934

Hi Ralph,

bugfix has been overwritten, see:

https://github.com/zendframework/zf2/…

Hi Hasan

What's the problem?

Your patch is intact.

I see two existing problems with Pdo\Connection::getDefaultSchema()

1) It uses "SELECT DATABASE()" to get the current schema, afaik this only works on MySQL. The SQL Standard says it should be "SELECT CURRENT_SCHEMA". PostgreSQL follows the standard in this respect.

2) It uses the method PDOStatement::fetch_row() , which doesn't exist. This appears to have been copy/pasted from Mysqli\Connection. This method should use PDOStatement::fetchColumn()

Both of these are addressed in PR 1485. The PR is having a tough time getting reviewed by Ralph, as it's so large.

This issue was resolved in PR #1613