duanqian9593 2013-09-06 05: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 06: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...

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部