Laravel 中的 mergeBindings 是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34147224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what the meaning mergeBindings in laravel
提问by b4dQuetions
hy I'm new in laravel 4 and I have found code like this
hy 我是 laravel 4 的新手,我找到了这样的代码
$sub = Abc::where(..)->groupBy(..); // Eloquent Builder instance
$count = DB::table( DB::raw("({$sub->toSql()}) as sub") )
->mergeBindings($sub->getQuery())
->count();
my quetion is
1. what the meaning mergeBindings($sub->getQuery())and give me example for using mergeBindings
我
的问题是1.mergeBindings($sub->getQuery())的含义是什么,并举例说明使用mergeBindings
回答by Ahmed Saad
assume your first query is like this:
假设您的第一个查询是这样的:
$sub = Abc::where('type', 1)->groupBy(..);
$sub = Abc::where('type', 1)->groupBy(..);
then when we convert it to sql:
然后当我们将其转换为 sql 时:
$sub->toSql();
$sub->toSql();
this will return query string some thing like this:
这将返回查询字符串这样的东西:
SELECT * FROM abc where abc.type = ? GROUp BY ...
SELECT * FROM abc where abc.type = ? GROUp BY ...
you can see the "?" as the type value which will be binded (replaced by 1) when the PDO executes that query string
你可以看到“?” 作为 PDO 执行该查询字符串时将绑定(替换为 1)的类型值
So when we use that sub query in another query as you do in
所以当我们在另一个查询中使用该子查询时
$count = DB::table( DB::raw("({$sub->toSql()}) as sub") )
->mergeBindings($sub->getQuery())
->count();
you have converted the first sub query to sql string, BUT the second query does not know any thing about your first sub query binding which in this example the value 1
您已将第一个子查询转换为 sql 字符串,但第二个查询对您的第一个子查询绑定一无所知,在本例中为值 1
so we need to merge the binding from the first sub query into the last query
so that the last query when executes it will know the value 1
and binds it to the
where clause in replace of "?", and your final executed query will be something like this
所以我们需要将第一个子查询中的绑定合并到最后一个查询中,以便最后一个查询在执行时知道该值1
并将其绑定到代替“?”的 where 子句,并且您最终执行的查询将是一些东西像这样
(SELECT count(*) from abc where type = 1 GROUP BY ...) as sub
(SELECT count(*) from abc where type = 1 GROUP BY ...) as sub
and thats it the use of mergeBindings()
method
这就是mergeBindings()
方法的使用
i hope this makes things clear for your question.
我希望这能让你的问题清楚。
thanks,
谢谢,