Hi,
I’m doing my first Pages out of the database adventure (Inspired by this awesome cookbook).
I noticed that when you use the paging your query in ->children()
always get re-executed completely. And I was wondering if there’s a way to optimise this.
One thing I noticed is that the panel, when using paging, always send a get parameter page
and I tried to limit my queries so I don’t always query everything but only the active data. Only thing is that when I do that, the paging (obviously) disappears because there aren’t as many records any more.
So my main question is: Is there any way to optimise your data retrieval in the panel when retrieving data from a database.
If there are also some other cool tips&tricks about working with a database as pages, feel free to share
<?php
use Kirby\Uuid\Uuid;
use Kirby\Cms\Content;
use App\Models\Customer;
class CustomersPage extends Kirby\Cms\Page
{
public function children()
{
if(!empty($page = get('page')))
{
$page--;
$users = Customer::limit(20)->offset($page*20)->orderBy('firstname', 'desc')->get();
}
else
{
$users = Customer::orderBy('firstname', 'desc')->get();
}
ray($users->count());
foreach ($users as $user) {
$customers[] = [
'slug' => 'cid-'.$user->id,
'num' => null,
'template' => 'customer',
'model' => 'customer',
'content' => [
'cid' => $user->id,
'uuid' => Uuid::generate(),
'title' => $user->firstname.' '.$user->lastname,
'firstname' => $user->firstname,
'lastname' => $user->lastname,
'email' => $user->email,
'company_name' => $user->company_name,
'delivery_street' => $user->delivery_street,
'delivery_street_nr' => $user->delivery_street_nr,
'delivery_zip' => $user->delivery_zip,
'delivery_city' => $user->delivery_city,
'vat' => $user->vat,
'phone' => $user->phone,
'fleet_discount_perc' => $user->fleet_discount_perc,
'is_dealer' => $user->is_dealer,
'gdpr_optin' => $user->gdpr_optin,
'created_at' => $user->created_at,
]
];
}
return Pages::factory($customers, $this);
}
}