Im trying to get the object value of structure field
kirby()->hook('panel.page.update', function($page) {
$getModulesList = $page->module_items()->toStructure();
var_dump($getModulesList);
exit();
if($courses = db::table('courses')->insert(array(
'modules_select_page' => $page,
'modules_select_value' => $value
)));
});
and im getting this.
object(Structure)#429 (3) {
[0]=>
object(Structure)#430 (1) {
["modules_select"]=>
object(Field)#431 (3) {
["page"]=>
string(33) "courses/cat-care-and-training-101"
["key"]=>
string(14) "modules_select"
["value"]=>
string(23) "Captive Animals Pathway"
}
}
[1]=>
object(Structure)#432 (1) {
["modules_select"]=>
object(Field)#433 (3) {
["page"]=>
string(33) "courses/cat-care-and-training-101"
["key"]=>
string(14) "modules_select"
["value"]=>
string(32) "Certificate II in Animal Studies"
}
}
[2]=>
object(Structure)#434 (1) {
["modules_select"]=>
object(Field)#435 (3) {
["page"]=>
string(33) "courses/cat-care-and-training-101"
["key"]=>
string(14) "modules_select"
["value"]=>
string(44) "Certificate III in Companion Animal Services"
}
}
}
now i want is to get the value from there and save it to database whenever the page is updated. I used hook to do that. How to get these value like “courses/cat-care-and-training-101” and “Certificate II in Animal Studies” etc. so i can insert them to db.
Since this is a structure field with multiple items, you have to loop through the items and then insert each line into the database:
kirby()->hook('panel.page.update', function($page) {
$getModulesList = $page->module_items()->toStructure();
foreach($getModulesList as $module):
if($courses = db::table('courses')->insert(array(
'modules_select_page' => $page->uri(),
'modules_select_value' => $module->modules_select()->value(),
)));
});
Note that you might end up with duplicated entries if you don’t check beforehand if these entries already exist.
Further information:
kirby()->hook('panel.page.update', function($page) {
$getModulesList = $page->module_items()->toStructure();
foreach ($getModulesList as $item) {
$getCoursesID = db::table('courses')->select(array('id'))->where(array('uri' => $page->uid()))->all();
db::table('courses_modules')->insert(array(
'course_id' => $getCoursesID->id(),
'module_id' => $item->modules_select()->value
));
}
im getting nulled in $getCoursesID->id()
I need to save all structures added the ( 5,6,7 ) in database
Are you saving the URI or the UID?
And supposing your $getCourseId
is an array, then you can’t just call the id
(of what?). That would only work if it was a single element.
Make a dump of getCoursesID
to see what you get.
heres the var_dump of getCoursesID
object(Collection)#483 (1) {
[0]=>
object(Obj)#484 (1) {
["id"]=>
int(91)
}
}
now my problem is how to get all the values in the structure. coz this $item->modules_select()->value only give me the first value.
i want to save them on db in this format
id | course_id | module_id
1 | 91 | 5
2 | 91 | 6
3 | 91 | 7
sorry im still learning in programming.
May I suggest that before you try to save anything in a database, you first familiarize yourself with the structure field using the documentation I linked to?
Try to output what you have in that field in a normal template, so that you get an idea how it works. I think we don’t get anywhere if the basics are missing and you are trying to do complicated stuff.
$item->modules_select()->value()
is called in a foreach loop, so in the first run of the loop it gives you the first value, then the second etc.
May I ask how advanced your PHP knowledge is? Do you know how to handle arrays etc.?
Maybe you can also post your blueprint yaml for the structure field here.