dongzhan0624 2019-02-15 11:52
浏览 49

在PHP中加入基于公共ID的多个集合

I have a big collections of authors with this structure:

- authors (id, profile_id, title, name) -> this are 590 authors

I have also 4 collections, with author.id == author_id

- sales (id, author_id, salesTotal) 
- subscribers (id, author_id, subscribersTotal)
- futureEvents (id, author_id, scheduledTotal)
- reservations (id, authors_id, reservationsTotal)

Not all authors have sales, subscribers, futureEvents and/or reservations. The collections are not organized in any particular order.

In order to build the authors Index view, I am making a FOREACH loop through all authors, so I need to join all those five collections into one like this:

- authors (id, profile_id, title, name, sales, subscribers, futureEvents, reservations)

How could I joint all those collections based in the author_id, having in mindt that each collection has different lenght and variate dinamically (new authors could sign in, new sales, subscribers sign in or out, etc) from day to day. So, the index can show different values from day to day

Any idea how?

EDIT: I am using LARAVEL, those collections are the result of each of a different query. For example:

$authorsSubscribers = DB::table('authors')
        ->join('subscriptions', 'subscriptions.author_id', '=', 'authors.id')
        ->groupBy('authors.title')
        ->selectRaw('authors.id, COUNT(subscriptions.author_id) AS Subscribers')
        ->get(); 

As result, my collections have all similar structure, like this one:

Collection {#86071 ▼
  #items: array:5 [▼
    0 => {#86015 ▼
      +"id": 16
      +"Subscribers": 269
    }
    1 => {#86016 ▼
      +"id": 49
      +"Subscribers": 269
    }
    2 => {#86017 ▼
      +"id": 20
      +"Subscribers": 269
    }
    3 => {#86018 ▼
      +"id": 10
      +"Subscribers": 269
    }
    4 => {#86019 ▼
      +"id": 11
      +"Subscribers": 269
    }
  ]
}

I can not make just one big giant query, because I am grouping (GroupBy, counting (COUNT), adding (SUM) and so on.

  • 写回答

1条回答 默认 最新

  • dongzi9196 2019-02-15 14:24
    关注

    I am just adding the solution I have found. perhaps is useful to anybody.

    I have trest array:merge, array_map and array_combine without success. Since the arrays have different structure, lenght and keys it was not possible to map one to one.

    So, I just used a Foreach Loop to "loop" through each element of the big collection and second loops to loop through each element comparing if the author_id was equal and if yes append it as an atribute.

        foreach ($authors as $author  ) {
    
            foreach ($sales as $sale) {
                if ($author->id === $sale->authorId) {
                    $author['salesTotal'] = $sale->salesTotal;
                    $author['salesAmount'] = $sale->salesAmount;
                }
            }
    
            foreach ($futureEvents as $events) {
                if ($author->id === $events->authorId) {
                    $author['futureEvents'] = $events->futureEvents;
                }
            }
            foreach ($reservations as $reservation) {
                if ($author->id === $reservation->authorId) {
                    $author['reservations'] = $reservation->reservations;
                }
            }
        }
    

    In that way I just got ONE big collection to loop through in the frontend. Like this:

    Author {#43454 ▼
      #attributes: array:14 [▼
        "id" => 1
        "profile_id" => 4
        "title" => "Missourimacejkovic.giles"
        ... lot of other attributes ...
        "created_at" => "2019-02-09 18:49:16"
        "updated_at" => "2019-02-09 18:49:16"
        "deleted_at" => null
        "subscriptions_count" => 2
        "events_count" => 19
        "salesTotal" => "9086"
        "salesAmount" => 200
        "futureEvents" => 9
        "reservations" => 450
      ]
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)