dpwh11290 2015-10-06 03:44
浏览 34

Zend \ Db \ Sql \选择使用函数作为表

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于大棚监测的pcb板设计
    • ¥20 sim800c模块 at指令及平台
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计