问题描述
背景
我有一个列表模型,属于并拥有多个订阅者。当然,一个订阅者属于并有很多列表。
I have a List model which belongs to and has many Subscriber. And of course a Subscriber which belongs to and has many List.
问题
我想要从多个列表中加载所有订阅者。但是,我只对不同的用户感兴趣,因为用户可以拥有并且确实属于多个列表。
I want to eager-load all subscribers from multiple lists. BUT, I am only interested in distinct subscribers, since a subscriber can have and indeed belong to multiple lists.
我的尝试
我使用了 ()
方法,但这并没有产生任何喜悦。我也可以循环查看结果集,手动分割重复。只是想知道有没有办法让Laravel为我做肮脏的工作?
I have used the distinct()
method but this hasn't yielded any joy. And I can also loop through the result set to manually slice out duplicates. Just wondering if there is a way of letting Laravel do the dirty job for me?
代码
$lists = Input::get('listsarray');
$addresses = Addressbook::with(array('subscribers' => function($query)
{
$query->where('active', '=', 1);
// $query->distinct(); //This didn't work
}))->whereIn('id', $lists)->get();
推荐答案
通过加入检索唯一订阅者
我会尽力解释,使用列表
而不是地址簿
因为我真的不了解你的模型,可能会引起进一步的混淆。
Retrieving unique subscribers through join
I'll try my best to explain it using Lists
instead of Adressbook
, because I couldn't really understand your model and that may introduce further confusion.
如果我正确地理解了你的意见,那么你试图检索所有与 id IN listarray
的列表。在这种情况下,Eager Loading不是正确的方法。渴望加载用于为模型预加载关联的目的,以便稍后使用它们。在您的情况下,您不会检索列表
及其订阅者
,而是唯一的订阅者
If I understood your comments correctly, you are trying to retrieve all unique subscribers that have associations with lists of id IN listarray
. In that case, Eager Loading is not the proper way of doing that. Eager loading serves the purpose of pre-loading associations for a model, in order to use them later on. In your case, you are not retrieving Lists
and their Subscribers
, but rather the unique Subscribers
themselves.
$lists = Input::get('listsarray');
$subscribers = Subscriber::join('list_subscriber', 'list_subscriber.subscriber_id', '=', 'subscribers.id')
->whereIn('list_subscriber.list_id', $lists)
->groupBy('list_subscriber.subscriber_id')
->get(array('subscribers.*'));
如果您还希望检索与此类订阅者相关联的所有列表,您可以这样做:
If you also wish to retrieve all lists associated with such subscribers, you can do so:
// Simply include a with('lists') and Eloquent will eager load them
$subscribers = Subscriber::with('lists')
->join('list_subscriber', 'list_subscriber.subscriber_id', '=', 'subscribers.id')
->whereIn('list_subscriber.list_id', $lists)
->groupBy('list_subscriber.subscriber_id')
->get(array('subscribers.*'));
Eager Loading
简单地提高性能,你不需要使用不同的,Laravel已经做到了。口头表现如下:
Eager Loading
If it's a matter of simply improving performance, you don't need to use distinct, Laravel already does it. Eloquent behaves like this:
- 获取所有
列表
。 - 通过
列表迭代
构建一组唯一的list_ids
- 使用
获取全部
list_subscribers
WHERE list_id IN(X,Y,Z) - 通过所有
list_subscribers
迭代创建一个唯一的subscriber_id
- 获取使用
WHERE id IN(X,Y,Z)
- Fetchs all
lists
. - Iterates through
lists
building an array of uniquelist_ids
- Fetchs all
list_subscribers
usingWHERE list_id IN (X, Y, Z)
- Iterates through all
list_subscribers
building an array of uniquesubscriber_id
- Fetchs all
subscribers
usingWHERE id IN (X, Y, Z)
这篇关于雄辩的ORM渴望在许多关系中加载不同的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!