I'm trying to use Zend\Db\Sql\Select
to select the results of a sql function, then join other tables, sort, paginate etc. I realize there are solutions that involve building a statement from hand-written SQL, but I have helpers for sorting and pagination that work with Select and I'd like to utilize the select API.
Anyway, if I try to do something like this...
$select->from(new TableIdentifier("ufn_user_client($userId, $clientId)", 'my_schema')
... it quotes the entire function, including the parens, and I get ERROR: relation \"my_schema.ufn_user_client(3, 2)\" does not exist
since it quoted the entire thing. I've tried using just a string instead of TableIdentifier, Zend\Db\Expression, etc with similar results. How can I get this literal string in the FROM clause without it quoting the whole expression?
Here's a minimal example:
<?php
require_once 'vendor/autoload.php';
$config = [
'driver' => 'pdo_pgsql',
'host' => 'postgresvm.dev',
'username' => 'user',
'password' => 'pass',
'dbname' => 'mydb',
'port' => 5432
];
$dbAdapter = new \Zend\Db\Adapter\Adapter($config);
$sqlAdapter = new \Zend\Db\Sql\Sql($dbAdapter);
$select = new \Zend\Db\Sql\Select();
$select->from("my_schema.ufn_user_client(2, 3)");
$results = $dbAdapter->query(
$sqlAdapter->buildSqlString($select),
\Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
)->toArray();
var_dump($results);
which results in an error:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "my_schema.ufn_user_client(2, 3)" does not exist
Maybe this is specific to the postgres pdo driver and how it quotes identifiers?