为什么Laravel / Eloquent不能使用JOIN进行预先加载?

时间:2022-10-25 08:01:07
<?php

class Cat extends Eloquent {

    public function user() {
        return $this->belongsTo('User');
    }
}

class User extends Eloquent {

    public function cats() {
        return $this->hasMany('Cat');
    }
}

Now:

现在:

$cats = Cat::with('user')->get();

Performs 2 queries:

执行2个查询:

select * from `cats`
select * from `users` where `users`.`id` in ('1', '2', 'x')

Why can't it just do:

为什么不能这样做:

select * from cats inner join users on cats.user_id = users.id

For those saying that there are both id columns in the table, that could be easily avoided with aliases:

对于那些说表中有两个id列的人来说,使用别名可以很容易地避免这种情况:

select 
    c.id as cats__id,
    c.name as cats__name,
    c.user_id as cats__user_id,
    b.id as users__id,
    b.name as users__name
from cats c
inner join users b on b.id = c.user_id

UPDATE

UPDATE

Someone pointed out that Eloquent doens't know the columns of the tables from the models, but I guess they could provide a way to define them in the model so then it could use aliases and do a proper join instead of an extra query.

有人指出,Eloquent并不知道模型中表格的列,但我猜他们可以提供一种在模型中定义它们的方法,这样它就可以使用别名并进行正确的连接而不是额外的查询。

2 个解决方案

#1


7  

My guess is that this allows for eager loading multiple one to many relationships. Say, for instance, we also had a dogs table:

我的猜测是,这允许急切加载多个一对多的关系。比方说,我们也有一张狗桌:

class User extends Eloquent {

    public function cats() {
        return $this->hasMany('Cat');
    }

    public function dogs() {
        return $this->hasMany('Dog');
    }
}

Now we want to eager load them both with the User:

现在我们希望用户同时加载它们:

$users = User::with('cats','dogs')->get();

There is no join that would work to combine these into a single query. However, doing a seperate query for each "with" element does work:

没有联接可以将这些组合成单个查询。但是,对每个“with”元素执行单独查询确实有效:

select * from `users`
select * from `cats` where `user`.`id` in ('1', '2', 'x')
select * from `dogs` where `user`.`id` in ('1', '2', 'x') 

So, while this methodology may produce an extra query in some simple circumstances, it provides the ability to eager load more complex data where the join method will fail.

因此,虽然这种方法可能会在一些简单的情况下产生额外的查询,但它提供了在连接方法失败时急切加载更复杂数据的能力。

This is my guess as to why it is this way.

这是我猜测为什么会这样。

#2


0  

cats and users likely both have a column named id, leaving your suggested query ambiguous. Laravel's eager loading uses an additional query, but avoids this potential mishap.

猫和用户可能都有一个名为id的列,使您建议的查询不明确。 Laravel的急切加载使用了额外的查询,但避免了这种潜在的不幸事件。

#1


7  

My guess is that this allows for eager loading multiple one to many relationships. Say, for instance, we also had a dogs table:

我的猜测是,这允许急切加载多个一对多的关系。比方说,我们也有一张狗桌:

class User extends Eloquent {

    public function cats() {
        return $this->hasMany('Cat');
    }

    public function dogs() {
        return $this->hasMany('Dog');
    }
}

Now we want to eager load them both with the User:

现在我们希望用户同时加载它们:

$users = User::with('cats','dogs')->get();

There is no join that would work to combine these into a single query. However, doing a seperate query for each "with" element does work:

没有联接可以将这些组合成单个查询。但是,对每个“with”元素执行单独查询确实有效:

select * from `users`
select * from `cats` where `user`.`id` in ('1', '2', 'x')
select * from `dogs` where `user`.`id` in ('1', '2', 'x') 

So, while this methodology may produce an extra query in some simple circumstances, it provides the ability to eager load more complex data where the join method will fail.

因此,虽然这种方法可能会在一些简单的情况下产生额外的查询,但它提供了在连接方法失败时急切加载更复杂数据的能力。

This is my guess as to why it is this way.

这是我猜测为什么会这样。

#2


0  

cats and users likely both have a column named id, leaving your suggested query ambiguous. Laravel's eager loading uses an additional query, but avoids this potential mishap.

猫和用户可能都有一个名为id的列,使您建议的查询不明确。 Laravel的急切加载使用了额外的查询,但避免了这种潜在的不幸事件。