ZF2-202: PHP Fatal Error in Zend\Db\Adapter\Driver\Pdo\Connection::getDefaultSchema() when using a Postgresql database
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.