我已经将Laravel查询生成器分配给了一个变量.使用时会改变 [英] I've assigned Laravel Query Builder to a variable. It changes when being used

查看:41
本文介绍了我已经将Laravel查询生成器分配给了一个变量.使用时会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个为什么要问的问题,而不是如何做:")

it's a WHY-question, not How-to one:)

我已将查询巨石分配给变量$ query:

I have assigned a Query Bulder to a variable $query:

$query = table::where(['id'=>1, 'this_version'=> 1]);
$versions['slug1'] = $query->select('tourist_id', 'tourist_version')->get()->toArray();
print_r($versions);

输出带有2(!)个子数组的数组:

outputs array with 2(!) sub-arrays:

Array
(
    [slug1] => Array
        (
            [0] => Array
                (
                    [tourist_id] => 1
                    [tourist_version] => 1
                )

            [1] => Array
                (
                    [tourist_id] => 2
                    [tourist_version] => 1
                )

        )

)

但是,如果我在$ query声明和获取$ version [2]数组之间使用$ query添加另一行,我的$ version [2]输出将缩短为一维数组:

But if I add another line using $query between my $query declaration and it's usage in getting $version[2] array, my $version[2] output is shortened to a 1-dimensional array:

$query = previoustour2_tourist::where(['tour2_id'=>$tour->id, 'this_version'=> 1]);
// Added line:
$versions['slug0'] = $query->select('version_created')->first()->version_created;
//
$versions['slug1'] = $query->select('tourist_id', 'tourist_version')->get()->toArray();
print_r($versions);

输出(注意slug1现在只有1个嵌套数组):

outputs (note slug1 now has only 1 nested array):

Array
(
    [slug0] => 2017-08-08 08:25:26
    [slug1] => Array
        (
            [0] => Array
                (
                    [tourist_id] => 1
                    [tourist_version] => 1
                )

        )

)

看起来像这样的一行:

$versions['slug0'] = $query->select('version_created')->first()->version_created;

已在原始$ query中添加了"first()"方法.我是对的,如果是的话,为什么会发生?

has added "first()" method to the original $query . Am I right and, if yes, why does it happen?

推荐答案

好吧,这是因为默认情况下,PHP中的一个对象(在您的情况下是查询"构建器对象)是通过引用传递的.您可以在此处阅读有关此内容的更多信息: PHP OOP参考.

Well, this is because by default an object (in your case is the Query builder object) in PHP is passed by reference. You can read more about this here: PHP OOP References.

我引用以上引用:

PHP引用是一个别名,它允许两个不同的变量 写入相同的值.

A PHP reference is an alias, which allows two different variables to write to the same value.

将查询生成器对象传递给$query变量时,实际上只是将引用传递给该对象,而不是它的副本.

When you pass the query builder object to the $query variable, you actually just pass the reference to this object and not the copy of it.

$query = previoustour2_tourist::where(['tour2_id'=>$tour->id, 'this_version'=> 1]);

因此,当您在第二行调用first()方法时,它实际上会修改查询生成器对象.

So when you call the first() method on the second line, it actually modifies the query builder object.

$versions['slug0'] = $query->select('version_created')->first()->version_created;

因此将即将到来的查询结果限制为1.要变通解决此问题,您可以克隆这样的查询对象:

Thus causing the upcoming query result to be limited to 1. In order to work around this issue, you can clone the query object like this:

$query = previoustour2_tourist::where(['tour2_id'=>$tour->id, 'this_version'=> 1]);
$versions['slug0'] = (clone $query)->select('version_created')->first()->version_created;
$versions['slug1'] = (clone $query)->select('tourist_id', 'tourist_version')->get()->toArray();
print_r($versions);

希望获得帮助!

这篇关于我已经将Laravel查询生成器分配给了一个变量.使用时会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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