您可以在Laravel Eloquent中使用命名参数吗

编程入门 行业动态 更新时间:2024-10-24 14:20:16
本文介绍了您可以在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中使用命名参数吗

本文发布于:2023-10-23 06:04:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520010.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:您可以   参数   Laravel   Eloquent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!