Kirby 3 + mysql database - Creating pages from a database

Hi everyone,

I try to connect kirby3 to a database. The database contains event infos and i want to show an overview of all events and also create child pages for every event.

I worked with the Cookbook but run into some issues.

My template model “models/events.php” looks like this:

<?php

class EventsPage extends Kirby\Cms\Page
{
    public function children()
    {
        $events = [];

        foreach (Db::select('veranstaltung') as $event) {
            $events[] = [
                'slug' => $event->slug(), //ID + Titel unique
                'num' => 0,    
                'template' => 'events',
                'model'    => 'events',
                'content' => [
                    'id'     => $event->event_id(), //unique Event ID
                    'ip_titel'  => $event->event_title(), //Event Title
                    'ip_utitel'  => $event->event_subline(), // Event Subline
                    'tx_web_short' => $event->event_teaser(), // Event Teaser
                    'sl_kat' => $event->event_category(), // Event Category
                    'dt_datum' => $event->event_date(), // Event Date
                    'tx_prog' => $event->event_description(), // Event Descripton
                    'hm_einlass' => $event->event_opening(), // Event Doors open
                    'hm_beginn' => $event->event_start(), // Event beginns
                    'cr_preis_vvk' => $event->event_presale(), // pre sale price
                    'cr_preis_ak' => $event->event_boxoffice(), // Box Offcie price
                ]
            ];
        }

        return Pages::factory($events, $this);
    }
}

?>

and my template /templates/event.php looks like that:

<?php $events = Db::select('veranstaltung'); ?>

<?php snippet('header') ?>
<?php snippet('menu') ?>
<div class="container">
<div class="row">
<?php foreach ($page->children() as $event): ?>

    <div class="col s12 m8 offset-m2">
      <div class="card">
        <div class="card-image">
          <span class="card-title"><?= $event->event_title() ?></span>
        </div>
        <div class="card-content">
          <p><?= $event->event_description() ?></p>
        </div>
        <div class="card-action">
          <a href="<?= $event->url() ?>">Mehr...</a>
        </div>
      </div>
    </div>
<?php endforeach ?>
</div>
</div>
<?php snippet('footer') ?>

The result looks like this: This is just one card of all created events, but the look alle the same. except the url.

<div class="col s12 m8 offset-m2">
      <div class="card">
        <div class="card-image">
          <span class="card-title"></span>
        </div>
        <div class="card-content">
          <p></p>
        </div>
        <div class="card-action">
          <a href="https://kirby3-mysql:8890/events/100312_margie-kinsky">Mehr...</a>
        </div>
      </div>
    </div>

Only the slug() content was sent out of the db. I

just don’t think I see it now. That’s why I’m very grateful for a “second pair of eyes”.

You are calling fields like $event->event_description() when you have defined these fields:

 'id'     => $event->event_id(), //unique Event ID
                    'ip_titel'  => $event->event_title(), //Event Title
                    'ip_utitel'  => $event->event_subline(), // Event Subline
                    'tx_web_short' => $event->event_teaser(), // Event Teaser
                    'sl_kat' => $event->event_category(), // Event Category
                    'dt_datum' => $event->event_date(), // Event Date
                    'tx_prog' => $event->event_description(), // Event Descripton
                    'hm_einlass' => $event->event_opening(), // Event Doors open
                    'hm_beginn' => $event->event_start(), // Event beginns
                    'cr_preis_vvk' => $event->event_presale(), // pre sale price
                    'cr_preis_ak' => $event->event_boxoffice(), // Box Offcie price

so you have probably set them up the wrong way round?

If hm_einlass is your column name and you want to call it as $event->event_opening(), then your definition should look like this:

'event_opening' => $event->hm_einlass()
1 Like

thank you for the quick support. So now i unsterstand why
'slug' => $event->slug()
worked.

It’s me again :see_no_evil:,

after changing all the definitions i see all the events on the events.php page. I followed the Cookbook steps and added a blueprint for events.php called /site/blueprints/pages/events.yml

  title: Events

sections:
  events:
    headline: Events
    type: pages
    info: "{{ page.event_title }}"
    template: event

this is my /site/blueprints/pages/event.yml blueprint

title: Event
icon: 📢

options:
  title: false
  status: false
  url: false

fields:
  id:
    label: Event ID
    type: text
  
  event_title:
    label: Event Titel 
    type: text
  
  event_subline:
    label: Subline
    type: text
  
  event_teaser:
    label: Event Teaser
    type: textarea
  
  event_category:
    label: Event Kategorie
    type: text
  
  event_date:
    label: Datum
    type: date
  
  event_description:
    label: Beschreibung
    type: textarea
  
  event_opening:
    label: Einlass
    type: text
  
  event_start:
    label: Beginn
    type: text
  
  event_presale:
    label: Vorverkauf
    type: text
  
  event_boxoffice:
    label: Abendkasse
    type: text

In the panel i don’t see any page. In the beginning i set it to
template: events
by mistake and i saw all the events, but i was in a loop if i click on any event. The same loop i have
if i click on the link created by $event->url().

http://root.domain/events/123456-super-event/123456-super-event/....

Can this have anything to do with the slug? Originally the table had no slug column. So I created unique slugs in MySQL from the id and event title.

Thanks for the support already.

I think the problem is that in your events.php page model, you assign the events template instead of the event template:

 'slug' => $event->slug(), //ID + Titel unique
                'num' => 0,    
                'template' => 'events', // this should be event, not events
                'model'    => 'events', // same for the model
                'content' => [
1 Like