สร้างคำค้นหาแบบซ้อนที่ซับซ้อนซึ่งประกอบด้วยช่องและหลายหน้า และส่งคืนอาร์เรย์ที่สามารถแยกวิเคราะห์เป็น JSON ได้ นี่เป็นประโยชน์เต็มรูปแบบในการดึงข้อมูลสำหรับ SPA และ PWA
คุณสามารถใช้โมดูลเพื่อแปลงจาก ProcessWire Page หรือ PageArray หรือแม้แต่ RepeaterMatrixPageArrays ไปเป็นอาร์เรย์หรือ JSON แบบสอบถามสามารถซ้อนกันและมีการปิดเป็นฟังก์ชันโทรกลับ ฟิลด์บางประเภทจะถูกแปลงโดยอัตโนมัติ เช่น Pageimages หรือ MapMarker
ชำระเงินหน้าเผยแพร่และสมัครรับข้อมูลอัปเดต
ขอแนะนำให้ติดตั้งผ่านผู้ดูแลระบบ ProcessWire " Modules "> " Site "> " Add New "> " Add Module from Directory " โดยใช้ชื่อคลาส PageQueryBoss
ดาวน์โหลดไฟล์จาก Github หรือที่เก็บ ProcessWire: https://modules.processwire.com/modules/page-query-builder/
มีสองวิธีหลัก:
$page->pageQueryJson($query);
$page->pageQueryArray($query);
แบบสอบถามสามารถมีคู่คีย์และค่า หรือเฉพาะคีย์เท่านั้น สามารถซ้อนกันได้และมีการปิดสำหรับค่าไดนามิก เพื่อแสดงตัวอย่างสั้นๆ:
// simple query:
$query = [
'height',
'floors',
];
$pages->find('template=skyscraper')->pageQueryJson($query);
การสืบค้นสามารถซ้อนกัน มีชื่อหน้า ชื่อเทมเพลต หรือมีฟังก์ชันและตัวเลือก ProcessWire:
// simple query:
$query = [
'height',
'floors',
'images', // < some fileds contain default sub-queries to return data
'files' => [ // but you can also overrdide these defaults:
'filename'
'ext',
'url',
],
// Assuming there are child pages with the architec template, or a
// field name with a page relation to architects
'architect' => [ // sub-query
'name',
'email'
],
// queries can contain closure functions
'querytime' => function($parent){
return "Query for $parent->title was built ".time();
}
];
$pages->find('template=skyscraper')->pageQueryJson($query);
ชื่อช่อง เดียว ; height
หรือ floors
หรือ architects
โมดูลสามารถจัดการฟิลด์ต่อไปนี้:
ชื่อเทมเพลต ; skyscraper
หรือ city
ชื่อของเพจย่อย (page.child.name=pagename); my-page-name
ตัวเลือก ProcessWire ; template=building, floors>=25
ชื่อใหม่ สำหรับดัชนีที่ส่งคืนผ่านตัวคั่น #
:
// the field skyscraper will be renamed to "building":
$query = ["skyscraper`#building`"]
คีย์ใดๆ ที่ด้านบน (1-5) พร้อมด้วยอาร์เรย์แบบสอบถามย่อยที่ซ้อนกันใหม่:
$query = [
'skyscraper' => [
'height',
'floors'
],
'architect' => [
'title',
'email'
],
]
คีย์ที่มีชื่อและ ฟังก์ชันการปิด เพื่อประมวลผลและส่งกลับแบบสอบถาม การปิดรับวัตถุหลักเป็นอาร์กิวเมนต์:
$query = [
'architecs' => function($parent)
{
$architects = $parent->find('template=architect');
return $architects->arrayQuery(['name', 'email']);
// or return $architects->explode('name, email');
}
]
$query = [
'title',
'subtitle',
// naming the key invitation
'template=Invitation, limit=1#invitation' => [
'title',
'subtitle',
'body',
],
// returns global speakers and local ones...
'speakers' => function($page){
$speakers = $page->speaker_relation;
$speakers = $speakers->prepend(wire('pages')->find('template=Speaker, global=1, sort=-id'));
// build a query of the speakers with
return $speakers->arrayQuery([
'title#name', // rename title field to name
'subtitle#ministry', // rename subtitle field to ministry
'links' => [
'linklabel#label', // rename linklabel field to minlabelistry
'link'
],
]);
},
// Child Pages with template=Program
'Program' => [
'title',
'summary',
'start' => function($parent){ // calculate the startdate from timetables
return $parent->children->first->date;
},
'end' => function($parent){ // calculate the endate from timetables
return $parent->children->last->date;
},
'Timetable' => [
'date', // date
'timetable#entry'=> [
'time#start', // time
'time_until#end', // time
'subtitle#description', // entry title
],
],
],
// ProcessWire selector, selecting children > name result "location"
'template=Location, limit=1#location' => [
'title#city', // summary title field to city
'body',
'country',
'venue',
'summary#address', // rename summary field to address
'link#tickets', // rename ticket link
'map', // Mapmarker field, automatically transformed
'images',
'infos#categories' => [ // repeater matrix! > rename to categories
'title#name', // rename title field to name
'entries' => [ // nested repeater matrix!
'title',
'body'
]
],
],
];
if ($input->urlSegment1 === 'json') {
header('Content-type: application/json');
echo $page->pageQueryJson($query);
exit();
}
การตั้งค่าโมดูลเป็นแบบสาธารณะ สามารถแก้ไขได้โดยตรง เช่น
$modules->get('PageQueryBoss')->debug = true;
$modules->get('PageQueryBoss')->defaults = []; // reset all defaults
ประเภทฟิลด์หรือเทมเพลตบางประเภทมาพร้อมกับตัวเลือกเริ่มต้น เช่น Pageimages เป็นต้น ซึ่งเป็นข้อความค้นหาเริ่มต้น:
// Access and modify default queries: $modules->get('PageQueryBoss')->defaults['queries'] = …
public $defaults = [
'queries' => [
'Pageimage' => [
'basename',
'url',
'httpUrl',
'description',
'ext',
'focus',
],
'Pageimages' => [
'basename',
'url',
'httpUrl',
'description',
'ext',
'focus',
],
'Pagefile' => [
'basename',
'url',
'httpUrl',
'description',
'ext',
'filesize',
'filesizeStr',
'hash',
],
'Pagefiles' => [
'basename',
'url',
'httpUrl',
'description',
'ext',
'filesize',
'filesizeStr',
'hash',
],
'MapMarker' => [
'lat',
'lng',
'zoom',
'address',
],
'User' => [
'name',
'email',
],
],
];
ค่าเริ่มต้นเหล่านี้จะถูกใช้เฉพาะในกรณีที่ไม่มีแบบสอบถามย่อยที่ซ้อนกันสำหรับประเภทที่เกี่ยวข้อง หากคุณสืบค้นฟิลด์ที่มีข้อมูลที่ซับซ้อนและไม่ได้ระบุแบบสอบถามย่อย แบบสอบถามนั้นจะถูกแปลงตาม:
$page->pageQueryArry(['images']);
// returns something like this
'images' => [
'basename',
'url',
'httpUrl',
'description',
'ext',
'focus'=> [
'top',
'left',
'zoom',
'default',
'str',
]
];
คุณสามารถระบุแบบสอบถามย่อยของคุณเองได้เสมอ ดังนั้นค่าเริ่มต้นจะไม่ถูกนำมาใช้:
$page->pageQueryArry([
'images' => [
'filename',
'description'
],
]);
คุณยังสามารถแทนที่ค่าเริ่มต้นได้อีกด้วย
$modules->get('PageQueryBoss')->defaults['queries']['Pageimages'] = [
'basename',
'url',
'description',
];
สามารถปรับดัชนีสำหรับองค์ประกอบที่ซ้อนกันได้ ซึ่งทำได้โดยใช้ค่าเริ่มต้นด้วย มี 3 ความเป็นไปได้:
นี่คือการตั้งค่าเริ่มต้น หากคุณมีฟิลด์ที่มีรายการย่อย ชื่อจะเป็นคีย์ในผลลัพธ์:
// example
$pagesByName = [
'page-1-name' => [
'title' => "Page one title",
'name' => 'page-1-name',
],
'page-2-name' => [
'title' => "Page two title",
'name' => 'page-2-name',
]
]
หากวัตถุแสดงอยู่ใน $defaults['index-id'] รหัสจะเป็นกุญแจสำคัญในผลลัพธ์ ในปัจจุบัน ไม่มีรายการใดที่แสดงเป็นค่าเริ่มต้นสำหรับดัชนีตามรหัส:
$modules->get('PageQueryBoss')->defaults['index-id']['Page'];
// example
$pagesById = [
123 => [
'title' => "Page one title",
'name' => 123,
],
124 => [
'title' => "Page two title",
'name' => 124,
]
]
ตามค่าเริ่มต้น ฟิลด์สองสามฟิลด์จะถูกแปลงโดยอัตโนมัติเพื่อให้มีดัชนีที่เป็นตัวเลข:
// objects or template names that should use numerical indexes for children instead of names
$defaults['index-n'] => [
'skyscraper', // template name
'Pageimage',
'Pagefile',
'RepeaterMatrixPage',
];
// example
$images = [
0 => [
'filename' => "image1.jpg",
],
1 => [
'filename' => "image2.jpg",
]
]
เคล็ดลับ: เมื่อคุณลบคีย์ Pageimage
ออกจาก $defaults['index-n'] ดัชนีจะเป็นชื่ออีกครั้ง
นี่เป็นฟังก์ชันการปิด helpfill บางส่วนที่คุณอาจต้องการใช้หรือสามารถช่วยเป็นจุดเริ่มต้นสำหรับตัวคุณเอง (แจ้งให้เราทราบหากคุณมีฟังก์ชันของคุณเอง):
$query = ['languages' => function($page){
$ar = [];
$l=0;
foreach (wire('languages') as $language) {
// build the json url with segment 1
$ar[$l]['url']= $page->localHttpUrl($language).wire('input')->urlSegment1;
$ar[$l]['name'] = $language->name == 'default' ? 'en' : $language->name;
$ar[$l]['title'] = $language->getLanguageValue($language, 'title');
$ar[$l]['active'] = $language->id == wire('user')->language->id;
$l++;
}
return $ar;
}];
การใช้โมดูล ContinentsAndCountries คุณสามารถแยกรหัส ISO และชื่อประเทศต่างๆ ได้:
$query = ['country' => function($page){
$c = wire('modules')->get('ContinentsAndCountries')->findBy('countries', array('name', 'iso', 'code'),['code' =>$page->country]);
return count($c) ? (array) $c[count($c)-1] : null;
}];
การใช้ RepeaterMatrix คุณสามารถสร้างสตริงเทมเพลตสำหรับส่วนหน้าของคุณได้ สิ่งนี้มีประโยชน์สำหรับปุ่ม ป้ายกำกับ ฯลฯ โค้ดต่อไปนี้ใช้รีพีตเตอร์ที่มี strings
ชื่อซึ่งมี key
และ body
เนื้อหา อาร์เรย์ที่ส่งคืนจะมีฟิลด์ key
ตามที่คุณเดา คีย์ และ body
เนื้อหาเป็นค่า:
// build custom translations
$query = ['strings' => function($page){
return array_column($page->get('strings')->each(['key', 'body']), 'body', 'key');
}];
การใช้การตั้งค่าต่อไปนี้คุณสามารถจัดการได้หลายภาษาและส่งคืนภาษาเริ่มต้นของคุณหากไม่มีภาษาที่ร้องขอ URL ประกอบด้วย: page/path/{language}/{content-type}
ตัวอย่างเช่น: api/icf/zurich/conference/2019/de/json
// get contenttype and language (or default language if not exists)
$lang = wire('languages')->get($input->urlSegment1);
if(!$lang instanceof Nullpage){
$user->language = $lang;
} else {
$lang = $user->language;
}
// contenttype segment 2 or 1 if language not present
$contenttype = $input->urlSegment2 ? $input->urlSegment2 : $input->urlSegment1;
if ($contenttype === 'json') {
header('Content-type: application/json');
echo $page->pageQueryJson($query);
exit();
}
โมดูลเคารพ wire('config')->debug มันทำงานร่วมกับ TracyDebug คุณสามารถแทนที่มันได้ดังนี้:
// turns on debug output no mather what:
$modules->get('PageQueryBoss')->debug = true;
ทำให้สามารถกำหนดค่าเริ่มต้นได้ผ่านแบ็กเอนด์ จะทำอย่างมีสไตล์ด้วยข้อความค้นหาเริ่มต้นได้อย่างไร
ดูไฟล์ใบอนุญาตที่รวมไว้สำหรับข้อความใบอนุญาตฉบับเต็ม
© noelboss.com