duanqian9593 2013-09-06 13:33
浏览 40
已采纳

你能在Laravel Eloquent中使用命名参数吗?

I use Oracle as a database layer, but the problem is that oracle via OCI8 (i made a PDO userspace driver) only supports named parameters in SQL statements and doesn't support positioned parameters. (Such as using multiple ?)

At a base, it's Laravel's Eloquent, that generates the SQL, but i can't find any documentation on how to override the construction of parameters.

I'd like to be able to create named parameters in the form of ":name" instead of placing numerous "?" throughout the query.

Can this be done? My guess is it has something to do with the Database Grammar classes...

  • 写回答

1条回答 默认 最新

  • dongquxiao8545 2013-09-06 14:10
    关注

    Oh well, if someone has a better solution, go ahead a submit it or maybe tell me what could be wrong with my temporary solution. I replace all "?" with ":autoparam" with a parameter increment creating ":autoparam0", ":autoparam1", ":autoparam2", etc.

        //Replace ? with a pseudo named parameter
        $newStatement = null;
        $parameter = 0;
        while($newStatement !== $statement)
        {
            if($newStatement !== null)
            {
                $statement = $newStatement;
            }
            $newStatement = preg_replace('/\?/', ':autoparam'.$parameter, $statement, 1);
            $parameter++;
        }
        $statement = $newStatement;
    

    Then, when i receive a request to bind a parameter from PDO, i check if the parameter is numeric. In most languages, as far as i know, numeric indexes are invalid identifiers, so i can safely assume, at least for my PDO Userspace driver that i can replace the numeric parameter name with:

        //Replace the first @oci8param to a pseudo named parameter
        if(is_numeric($parameter))
        {
            $parameter = ':autoparam'.$parameter;
        }
    

    It works for now, i need to do more tests with laravel to see if problem appears in a different score, but so far, it seems allright...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置