Zend pdo_oci和pdo_mysql生成不同的查询格式

时间:2019-03-06 12:05:57

标签: php mysql oracle zend-framework

Oracle数据库适配器:“ pdo_oci ” MySQL数据库适配器:“ pdo_mysql

用于建立JOIN查询的代码:

            $db = Zend_Db_Table::getDefaultAdapter();
            $db->setFetchMode(Zend_Db::FETCH_OBJ);

            $select = $db->select()->from(array(
                   't1' => $this->_Table1
            ));
            $select = $select->join(array(
                   't2' => $this->_Table2
           ), 't1.MENU_ID = t2.SUBMENU_ID',
            array(
                    'MENU_ID',
                    'SET_REDIRECT_URL',
                    'SUBMENU_TYPE'
            ));
            $select = $select->order(array(
                    'MENU_ID',
                    't2.DISPLAY_ORDER'
            ));
            $select = $select->where('t1.IS_ACTIVE = ?', 'Y');
            $result = $db->fetchAll($select);

由Oracle适配器(pdo_oci)生成的查询:

SELECT "t1".*, "t2"."MENU_ID" AS "parent_id", "t2"."SET_REDIRECT_URL", "t2"."SUBMENU_TYPE" FROM "DRP_ADMIN_MENU_MASTER" "t1"  INNER JOIN "DRP_ADMIN_MENU_MAPPER" "t2" ON t1.MENU_ID = t2.SUBMENU_ID WHERE (t1.IS_ACTIVE = 'Y') ORDER BY "parent_id" ASC, "t2"."DISPLAY_ORDER" ASC

由Mysql适配器(pdo_mysql)生成的查询:

SELECT `t1`.*, `t2`.`MENU_ID` AS `parent_id`, `t2`.`SET_REDIRECT_URL`, `t2`.`SUBMENU_TYPE` FROM `DRP_ADMIN_MENU_MASTER` AS `t1`  INNER JOIN `DRP_ADMIN_MENU_MAPPER` AS `t2` ON t1.MENU_ID = t2.SUBMENU_ID WHERE (t1.IS_ACTIVE = 'Y') ORDER BY `parent_id` ASC, `t2`.`DISPLAY_ORDER` ASC

问题:Oracle DB无法在[t1.MENU_ID = t2.SUBMENU_ID]中标识t1和t2,除非它们用双引号引起来。而且,如果我手动输入双引号,则mysql会出错。

Oracle中的错误:

  

一般错误:904 OCIStmtExecute:ORA-00904:“ T2”。“ SUBMENU_ID”:   无效的标识符

1 个答案:

答案 0 :(得分:0)

解决了。使用$ db-> quoteIdentifier()。而且有效

        $db = Zend_Db_Table::getDefaultAdapter();
        $db->setFetchMode(Zend_Db::FETCH_OBJ);

        $select = $db->select()->from(array(
               't1' => $this->_Table1
        ));
        $select = $select->join(array(
               't2' => $this->_Table2
       ), $db->quoteIdentifier('t1.MENU_ID') . ' = ' . $db->quoteIdentifier('t2.SUBMENU_ID'),
        array(
                'MENU_ID',
                'SET_REDIRECT_URL',
                'SUBMENU_TYPE'
        ));
        $select = $select->order(array(
                'MENU_ID',
                't2.DISPLAY_ORDER'
        ));
        $select = $select->where($db->quoteIdentifier('t1.IS_ACTIVE') . ' = ?', 'Y');
        $result = $db->fetchAll($select);