# Mix and sort multiple models/blueprints

**URL:** <https://forum.getkirby.com/t/mix-and-sort-multiple-models-blueprints/17951>\
**Category:** Questions\
**Tags:** v3\
**Created:** [April 20, 2020, 3:37pm UTC](https://forum.getkirby.com/t/mix-and-sort-multiple-models-blueprints/17951 "2020-04-20T15:37:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbidoret](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jbidoret/32/9445_2.png) [@jbidoret](https://forum.getkirby.com/u/jbidoret)\
**Post date:** [April 20, 2020, 3:37pm UTC](https://forum.getkirby.com/t/mix-and-sort-multiple-models-blueprints/17951/1 "2020-04-20T15:37:15Z")

</div>

Hi.

I’m looking to set up a calendar that can order content from different page templates.

I have several blueprints (`shows` and `rendezvous`), which have date informations and are stored in multiple directories. In `shows`, the dates are stored in a structure (several dates are possible). In `rendezvous`, only one date is possible.

I’d like to mix these two types of contents together and to sort them by date, so that I have:

- 26/04/2020
  - 19:00 – [rendezvous 1]
  - 20:30 – [show 1]
  - 22:00 – [rendezvous 2]

- 28/04/2020
  - 14:30 – [show 2]
  - 20:30 – [show 1]

- 05/05/2020
  - 15:30 – [rendezvous 3]

Notes:

- I may have performance issues (relying on $site-\>index() filtered by templates array), because there will be a lot of events
- All events should be future events (not difficult, but I failed to solve via custom pages models)
- I must duplicate shows display on multiple days, according to each date.

I can’t figure out where to start…  
If anyone here can help me, thank you in advance.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 20, 2020, 7:26pm UTC](https://forum.getkirby.com/t/mix-and-sort-multiple-models-blueprints/17951/2 "2020-04-20T19:26:00Z")

</div>

> [@jbidoret](#):
>
> I may have performance issues (relying on $site-\>index() filtered by templates array), because there will be a lot of events

What numbers are we talking here? A custom cache for the events might be useful if there are two many. Avoid `$site->index()` if possible.

Yes, you can merge structure items with pages by creating a single collection from array items.

> [@jbidoret](#):
>
> I must duplicate shows display on multiple days, according to each date.

How exactly are these stored? Could you post the blueprint?

---

<div class="post-metadata">

**Author:** ![jbidoret](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jbidoret/32/9445_2.png) [@jbidoret](https://forum.getkirby.com/u/jbidoret)\
**Post date:** [April 21, 2020, 11:04am UTC](https://forum.getkirby.com/t/mix-and-sort-multiple-models-blueprints/17951/3 "2020-04-21T11:04:58Z")

</div>

Hi Sonja.

Thank you for your answer.  
Regarding the potential performance issues, I’ll try to deal with a custom cache.

> [@texnixe](#):
>
> Yes, you can merge structure items with pages by creating a single collection from array items.

Shows belong to a “season”, they’re stored in `/content/seasons/2019-2020/`. Rendezvous are in `/content/rendezvous/`. To group shows by day, I used the solution you provided here: [Create a collection of pages from a structure field - #10 by texnixe](https://forum.getkirby.com/t/create-a-collection-of-pages-from-a-structure-field/8591/10).

```auto
# controllers/calendar.php

return function ($site, $page, $kirby) {

  // get future shows
  $shows = page("seasons/2019-2020")->children()->listed()->filter(function ($child) {
      # what should I use to grab only future shows ?
  });

  // get all dates
  foreach($shows as $show) {
      $dates = $show->dates()->toStructure()->pluck('date');
      sort($dates);
      foreach($dates as $date) {
          $mydates[] = strtotime($date);
      }        
  }

  // deduplicate + sort
  $mydates = array_unique($mydates, SORT_NUMERIC);
  sort($mydates);

  return [
      'shows' => $shows,
      'dates' => $mydates
  ];
};

function getShowsPerDate($shows, $day) {
    $showsPerDay = $shows->filter(function($child) use($day) {
        $dates = $child->dates()->toStructure()->pluck('date');
        foreach($dates as $date) {
            $strDates[] = strtotime($date);
        }
        // check if the current day is in dates
        if(in_array($day, $strDates)) {
            return $child;
        }
    });
    return $showsPerDay;
}

```

Then, in the template:

```auto
<?php foreach($dates as $date) :
    $dayShows = getShowsPerDate($shows, $date); ?>
    <?php foreach($dayShows as $show) :?>
        (…)

```

This logic works for shows (even if I thought I’d better have the final data prepared in the controller without calling getShowsPerDate in the loop), but I don’t know where neither how to plug the rendezvous…

> [@texnixe](#):
>
> How exactly are these stored? Could you post the blueprint?

Sure. Nothing fancy here (I simplified):

```auto
# show.yml
fields:
  dates:
    label: Dates
    type: structure
    fields:
      place:
        type: text
      date:
        type: date
        label: Date
        time: true

```

```auto
# rendezvous.yml
fields:
  place:
    type: text
  date:
    type: date
    label: Date
    time: true

```

Thanks a lot for your help. I hope I’ve been clear enough.
