nomad
April 25, 2023, 3:54pm
1
How do you input to query multiple YAML data items at once?
Current Controller:
<?php
return function ($site) {
$query = get('q');
$results = page('items-for-sale')->search($query, 'itemnumber');
$results = $results->paginate(15);
return [
'query' => $query,
'results' => $results,
'pagination' => $results->pagination()
];
};
I was thinking the following but it doesn’t work:
<?php
return function ($site) {
$query = get('q');
$results = page('items-for-sale')->search($query, 'itemnumber', 'shortdescription', 'fulldescription');
$results = $results->paginate(15);
return [
'query' => $query,
'results' => $results,
'pagination' => $results->pagination()
];
};
nomad
April 25, 2023, 3:58pm
2
I figured it out
<?php
return function ($site) {
$query = get('q');
$results = page('items-for-sale')->search($query, 'itemnumber|shortdescription|fulldescription');
$results = $results->paginate(15);
return [
'query' => $query,
'results' => $results,
'pagination' => $results->pagination()
];
};
bnomei
April 25, 2023, 4:19pm
3
hi jonathan,
in case you need the search to match terms by AND not ANY then you can chain the search on the results again and again. like must match “hello world” and not “hello” or “world”.
<?php
return function ($site) {
// AND-based search
$queryParts = explode(' ', $query);
$first = array_shift($queryParts);
$results = page('items-for-sale')
->search($first, 'itemnumber|shortdescription|fulldescription');
foreach ($queryParts as $q) {
$results = $results->search($q, 'itemnumber|shortdescription|fulldescription');
}
// ...
};
1 Like
nomad
April 25, 2023, 4:39pm
4
Thanks for this. It amazes me just how much Kirby can do, phenomenal product!