Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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 为什么Eloquent has和Where has方法使用子查询而不是联接?_Php_Laravel_Laravel 4_Eloquent - Fatal编程技术网

Php 为什么Eloquent has和Where has方法使用子查询而不是联接?

Php 为什么Eloquent has和Where has方法使用子查询而不是联接?,php,laravel,laravel-4,eloquent,Php,Laravel,Laravel 4,Eloquent,为什么Laravel对雄辩的has和whereHas方法使用子查询而不是使用联接?例如,我构建了一个雄辩的查询,如下所示: Order::whereHas('orderProducts', function($query) { $query->where('product_id', $this->id); }) ->whereHas('status', function($query) { $query->where('sales_dat

为什么Laravel对雄辩的
has
whereHas
方法使用子查询而不是使用联接?例如,我构建了一个雄辩的查询,如下所示:

Order::whereHas('orderProducts', function($query)
{
        $query->where('product_id', $this->id);

})
->whereHas('status', function($query)
{
        $query->where('sales_data', 1);

})
->where('ext_created_at', '>=', $startDate)
->where('ext_created_at', '<=', $endDate.' 23:59:59')
->distinct()
->count('id');
DB::table('orders')
    ->leftJoin('order_products', 'orders.id', '=', 'order_products.order_id')
    ->leftJoin('order_statuses', 'orders.order_status_id', '=', 'order_statuses.id')
    ->whereNull('orders.deleted_at')
    ->where('order_products.product_id', $this->id)
    ->where('orders.ext_created_at', '>=', $startDate)
    ->where('orders.ext_created_at', '<=', $endDate.' 23:59:59')
    ->whereNull('order_products.deleted_at')
    ->where('order_statuses.sales_data', 1)
    ->distinct()
    ->count('orders.id')
结果查询是:

select count(distinct `id`) as aggregate from `orders` where `orders`.`deleted_at` is null and (select count(*) from `order_products` where `order_products`.`order_id` = `orders`.`id` and `product_id` = ? and `order_products`.`deleted_at` is null) >= 1 and (select count(*) from `order_statuses` where `orders`.`order_status_id` = `order_statuses`.`id` and `sales_data` = ? and `order_statuses`.`deleted_at` is null) >= 1 and `ext_created_at` >= ? and `ext_created_at` <= ?
select count(distinct `orders`.`id`) as aggregate from `orders` left join `order_products` on `orders`.`id` = `order_products`.`order_id` left join `order_statuses` on `orders`.`order_status_id` = `order_statuses`.`id` where `orders`.`deleted_at` is null and `order_products`.`product_id` = ? and `orders`.`ext_created_at` >= ? and `orders`.`ext_created_at` <= ? and `order_products`.`deleted_at` is null and `order_statuses`.`sales_data` = ?

选择count(不同的`orders`.`id`)作为`orders`上的`orders`left join`order`products`.`id`=`order`products`.`order`id` left join`order`status`.`order`id`=`order` status`.`id`其中`orders`.`deleted`at`为空,而`order`products`.`products`.`id`=?和“订单”。`ext\u在“>=”处创建?和'orders`.'ext_created_at`可以添加许多子查询,但不能添加联接。这是为了避免程序员在一个查询中对同一关系使用
join
whereHas
方法时发生冲突。你可以用化名,但你有多安全?我不知道我明白你的意思。你能给我举个例子吗?还有,你所说的“但是你有多远是安全的?”是什么意思?在laravel中,你可以有多个对象链接。例如:
Car::whereHas('engine',function($q){return$q->whereHas('pistone',function($q){return$q->whereHas('feel'…}}});
您得到了这个想法。通过laravel实现(子查询)查询的范围很容易被分开。通过加入,并不是很多。我们还必须考虑到LaaFIEW为我们抽象所有的数据库实现。明天,你想从MySQL切换到PASGRESs。一切都会顺利地进行。因此,在实现<代码>之前,有很多因素需要考虑。代码>方法。不幸的是,whereHas()方法会在数据库扩展时导致死角。30k注册用户和Mysql需要将近两分钟的时间,才能使用嵌套的whereHas()子句,基于复合多对多关系约束筛选其索引视图。联接似乎是唯一的生存方法。。。