CHOICES------------idquestion_id_fktextQUESTIONS----------------idquiz_id_fktextcorrectanswerQUIZZES------------idnamedescription
$quiz = new \DB\SQL\Mapper($db,'quizzes');
...
$quiz->save();
$question = new \DB\SQL\Mapper($db,'questions');
$choice = new \DB\SQL\Mapper($db,'choices');
foreach($q_data as $q) {
$question->reset();
...
$question->quiz_id_fk = $quiz->_id;
$question->save();
foreach($c_data as $c) {
$choice->reset();
...
$choice->question_id_fk = $question->_id;
$choice->save();
}
}
To get the ID of the last inserted row or the last value from a sequence object, you must use the reserved $key
value = '_id'
:
$lastInsertedID = $mapper->get('_id'); // get the ID of the last inserted row or the last value from a sequence object
function save($f3){
$newquiz = new Quizzes ($this->db);
$newquiz->copyfrom('POST', function($val) {
return array_intersect_key($val, array_flip(array('name','description')));
});
$newquiz->save();
// this code above works properly because the field names in my form are just 'name' and 'description' which are unique
$question = new Questions ($this->db);
for ($i=1; $i<11; $i++) {
$question->reset();
$question->quiz_id_fk = $newquiz->_id;
$question->copyfrom('POST', function($val) {
return array_intersect_key($val, array('text'=>'question'.$i.'_text', 'correctanswer'=>'question'.$i.'_correctanswer'));
});
// this part fails because my form has 10 inputs for text, byline, and correctanswer. in the form, each field is named 'question1_text', 'question1_correctanswer' / 'question2_text', and so on, where the fields use a named using a simple loop with {{@i}}. However, when I try to parse the POST values, I'm not sure how to extract the value from 'question1_text' and save it into just 'text' in the database record, and to extract the value from 'question1_correctanswer' from POST and save it into just 'correctanswer' for the database.
$question->save();
}
$f3->reroute('/admin');
}
userID
), the contents of that input field is transferred to $_POST['userID']
, duplicated by F3 in its POST.userID
variable, and saved to the mapped field $user->userID
in the database. The process becomes very simple if they all have identically-named elements. Consistency in array keys, i.e. template token names, framework variable names and field names is key :)"