您可以在Laravel Eloquent中使用命名参数吗 [英] Can you use named parameters in Laravel Eloquent

查看:75
本文介绍了您可以在Laravel Eloquent中使用命名参数吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Oracle用作数据库层,但问题是通过OCI8(我将其制成PDO用户空间驱动程序)的oracle仅支持SQL语句中的命名参数,而不支持定位的参数. (例如使用多个?)

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 ?)

从根本上讲,是Laravel的Eloquent生成SQL,但我找不到任何有关如何重写参数构造的文档.

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.

我希望能够以:name"的形式创建命名参数,而不是放置大量的?"整个查询中.

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...

推荐答案

哦,如果有人有更好的解决方案,请继续提交它,或者告诉我我的临时解决方案可能出了什么问题.我替换所有的?"通过:autoparam"和参数增量创建:autoparam0",:autoparam1",:autoparam2"等.

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;

然后,当我收到来自PDO的绑定参数的请求时,我会检查该参数是否为数字.据我所知,在大多数语言中,数字索引是无效的标识符,因此,至少对于我的PDO Userspace驱动程序,我可以安全地假定我可以将数字参数名称替换为:

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;
    }

现在可以使用,我需要对laravel进行更多测试,以查看问题是否以不同的分数出现,但是到目前为止,看来还可以...

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...

这篇关于您可以在Laravel Eloquent中使用命名参数吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆