Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-停止多个查询链接_Php_Mysql_Sql_Laravel_Laravel 4 - Fatal编程技术网

Php Laravel-停止多个查询链接

Php Laravel-停止多个查询链接,php,mysql,sql,laravel,laravel-4,Php,Mysql,Sql,Laravel,Laravel 4,目前,我可以使用查询生成器从数据库中获取一些统计数据,而不是雄辩的 假设我想得到拥有iOS令牌的用户数,然后是在我的一个表中拥有android令牌的用户数 class Connection { public function __construct() { return $this->db = DB::connection('database_one'); } } class Statistics extends Connection {

目前,我可以使用查询生成器从数据库中获取一些统计数据,而不是雄辩的

假设我想得到拥有iOS令牌的用户数,然后是在我的一个表中拥有android令牌的用户数

class Connection {
    public function __construct()
    {
        return $this->db = DB::connection('database_one');
    }
}

class Statistics extends Connection {
    public function devices()
    {
        $this->devices = $this->db->table('users_devices');

        $arr = [
            'ios' => $this->devices->where('ios_push_token', '!=', '')->count(),
            'android' => $this->devices->where('android_push_token', '!=', '')->count(),
        ];

        return $arr;        
    }
}
获取
ios
设备的查询是正确的:

select count(*) as aggregate from `users_devices` where `ios_push_token` != ?
array (size=1)
      0 => string '' (length=0)
但是,我随后遇到了一个问题,即
android
值,查询尝试执行:

select count(*) as aggregate from `users_devices` where `ios_push_token` != ? and `android_push_token` != ?
array (size=2)
  0 => string '' (length=0)
  1 => string '' (length=0)
它似乎将where子句从第一个查询链接到第二个查询,如此类推,因为多个实例提供了不正确的数据

我认为这与使用DB::connection的一个实例有关,但我不确定?

如何:

class Statistics {
    public function devices()
    {
        $arr = [
            'ios' => DB::table('users_devices')->where('ios_push_token', '!=', '')->count(),
            'android' => DB::table('users_devices')->where('android_push_token', '!=', '')->count(),
        ];

        return $arr;        
    }
}
或克隆对象:

class Connection {
    public function __construct()
    {
        return $this->db = DB::connection('database_one');
    }
}

class Statistics extends Connection {
    public function devices()
    {
        $this->devices = $this->db->table('users_devices')->remember(30); // etc.

        $ios = clone $this->devices;
        $android= clone $this->devices;

        $arr = [
            'ios' => $ios->where('ios_push_token', '!=', '')->count(),
            'android' => $android->where('android_push_token', '!=', '')->count(),
        ];

        return $arr;        
    }
}
那么:

class Statistics {
    public function devices()
    {
        $arr = [
            'ios' => DB::table('users_devices')->where('ios_push_token', '!=', '')->count(),
            'android' => DB::table('users_devices')->where('android_push_token', '!=', '')->count(),
        ];

        return $arr;        
    }
}
或克隆对象:

class Connection {
    public function __construct()
    {
        return $this->db = DB::connection('database_one');
    }
}

class Statistics extends Connection {
    public function devices()
    {
        $this->devices = $this->db->table('users_devices')->remember(30); // etc.

        $ios = clone $this->devices;
        $android= clone $this->devices;

        $arr = [
            'ios' => $ios->where('ios_push_token', '!=', '')->count(),
            'android' => $android->where('android_push_token', '!=', '')->count(),
        ];

        return $arr;        
    }
}

只要使用有意义的名字
$this->devices
不是
devices
,而是
Query\Builder
实例,因此显然需要两个这样的实例进行单独的查询。只需使用有意义的名称即可
$this->devices
不是
devices
,而是
Query\Builder
实例,因此显然需要两个这样的实例来进行单独的查询。虽然这样做很有效,但我希望能够使用上面的方法,然后我可以很容易地在声明表后附加类似
->记住(30)
。。。如果我必须多次这样做,并且一遍又一遍地为同一张表命名,那就不太理想了。为编辑干杯。所以克隆可以工作:)可惜我不能将所有变量分配给一个克隆(我很懒)。你也可以创建一个私有方法来获取设备表,并调用$this->devicesTable()->where(…)虽然这可以工作,但我希望能够实现上面的方法,然后我可以轻松附加类似
->记住(30)
在我声明表的位置之后。。。如果我必须多次这样做,并且一遍又一遍地为同一张表命名,那就不太理想了。为编辑干杯。所以克隆可以工作:)可惜我不能将所有变量分配给一个克隆(我很懒)。你也可以创建一个私有方法来获取设备表,并调用$this->devicesTable()->where(…)