} catch (\Exception $e) { attd_error(HTTP_Status::BAD_REQUEST, "Invalid lecture date format."); } if ($lec_date < $st_date || $lec_date > $today) { attd_error(HTTP_Status::BAD_REQUEST, "Lecture date must be within " . $st_date->format("d-m-Y") . " to " . $today->format("d-m-Y") . "."); } $lec_date = $lec_date->format("Y-m-d"); try { $db = new MyDbCon(); $db->beginTransaction(); $obj = new Lectures(array('lec_date' => $lec_date, 'attd_mst_id' => $queries['mst'])); $db->insert($obj); $db->prepare(); $db->execute(); $lec_id = $db->getLastGeneratedValue(); $db->setInsert(new Attendance()); $abs_objs = array(); foreach ($students['data'] as $stud) { //$ids[]=$stud['stud_id']; $presence = 1; if (in_array($stud['stud_id'], $abs_ids)) { $presence = 0; } $abs_objs[] = new Attendance(array('lec_id' => $lec_id, 'stud_id' => $stud['stud_id'], 'presence' => $presence)); } $db->multiInsert($abs_objs); $db->execute(); $db->commit(); echo json_encode(array('done' => true, 'final' => 'Attendance Added Successfully!')); } catch (\Exception $e) { $db->rollback();